2012-08-19 76 views
0

這是我如何調用Java腳本函數在我的HTML頁面: -圖片滾動不工作

<script type="text/javascript" src="imagerollover.js"></script> 

</script> 
<script type="text/javascript"> 
//Following function should be called at the end of the page: 
imagerollover(); 
</script> 
</head> 

<body> 
. 
. 
. 

這是我imagerollover.js: -

function imagerollover(){ 

    var allimages=document.getElementsByTagName("img") 
    var preloadimages=[] 
    for (var i=0; i<allimages.length; i++){ 
     if (allimages[i].getAttribute("data-over")){ //if image carries "data-over" attribute 
      preloadimages.push(new Image()) //preload "over" image 
      preloadimages[preloadimages.length-1].src=allimages[i].getAttribute("data-over") 
      allimages[i].onmouseover=function(){ 
       this.src=this.getAttribute("data-over") 
      } 
      allimages[i].onmouseout=function(){ 
       this.src=this.getAttribute("data-out") 
      } 
     } //end if 
    } //end for loop 
} 

//Usage: Call following function at the end of the page: 
//imagerollover() 

圖片代碼:

<img src="knitting1.jpg" 
data-over="knitting2.jpg" 
data-out="knitting1.jpg" 
alt="Logo" width="400px" height="90" align="middle" /> 

此外,我的網站的根文件夾中存在兩個圖像knitting1.jpg和knitting2.jpg。我不知道什麼是錯的。請幫忙。謝謝。

我在開始的時候放置一個警告imagerollover.js文件中。它正在呼叫,但它沒有顯示任何翻滾效果。爲什麼?

+0

你的''標籤是什麼樣子? – Blender 2012-08-19 05:27:36

+0

Logo Serenity 2012-08-19 05:36:30

回答

2

你需要運行這個onload事件 - 您的圖片標籤還沒有可用於腳本,當你執行的功能 - 你是不是應該被放置在頁面或中端腳本的指令後實際我的建議在onload

<head> 
<script type="text/javascript" src="imagerollover.js"></script> 

</script> 
<script type="text/javascript"> 
window.onload=function() 
    //Following function should be called at the end of the page OR on window.onload! 
    imagerollover(); 
} 
</script> 
</head> 
+0

是,當所有的圖像出現在文檔window.load事件觸發加載。 – 2012-08-19 05:37:08