2010-11-12 72 views
0

嗨我正在一個網站上工作。我需要幫助使文本出現在div中,它表示任何圖像點擊了它們的標題和大小。在DOM腳本中。誰能幫忙?沒有innerhtml。如何在Dom中顯示描述文本圖像3秒鐘?

感謝

+0

通常你會得到幫助這裏固定的東西你不工作。你嘗試過什麼? – lincolnk 2010-11-12 19:44:36

+0

功能showCredit(whichpic,X,Y){ \t VAR源= whichpic.getElementsByTagName (「img」); \t var title = source.getAttribue(「src」); \t var size = source.getAttribute(「width」,「height」); \t \t document.write(title + size); \t } \t \t 功能prepareTimers(){ \t \t \t \t的setTimeout(showCredit,4000); \t \t \t} \t \t \t的window.onload = prepareTimers – 2010-11-12 19:52:25

回答

1

使用純DOM腳本,並像jQuery沒有幫手框架,總得灰塵有些東西我還沒有在一段時間使用!

這就是說這裏雅去。必須在頁面加載後放置。 (或刪除最後一個「showCredit();」線,並將其放在你的身上onload。

注意你需要改變這一點,我只是把「源」放在文本中,其他屬性和樣式是給你。

function showCredit(){ 

    //find all image tags we want 
    var elements = document.getElementsByTagName('img') 

    //iterate through them 
    for(var i=0; i<elements.length;i++){ 
     //bind the onclick function to all image tags 
     elements[i].onclick=function(){ 
      //create the new div 
      var el = document.createElement('div') 

      //alter this to be whatever text you want 
      var text = document.createTextNode('Source = '+this.getAttribute('src')); 

      //alter this if you're going to have more than one clickable div 
      el.id = 'test'; 

      //add the text to the div 
      el.appendChild(text); 

      //add the new div after the image tag 
      this.parentNode.insertBefore(el, this.nextSibling); 

      //set a timer to find the element we've named "test" and remove it 
      window.setTimeout(function(){ 
       var element = document.getElementById('test'); 
       if(element){ 
        element.parentNode.removeChild(element); 
       } 
      }, 4000); 
    } 
    } 
} 

//execute the function (bind all images) 
showCredit(); 
+0

大代碼,以完成這裏所述OP請求是標題和大小的行:document.createTextNode( '源= '+ this.getAttribute(' SRC')+ ',title:'+ this.title +',size:'+ this.offsetWidth +'x'+ this.offsetHeight); – 2010-11-12 23:09:59