我試圖在兩個div上交換兩個圖像,因此22a.jpg以div#second結尾,22b.jpg結束於div#第一次,但每次我點擊「交換」按鈕,我在Firebug中得到這個錯誤:imgArray [2] .src未定義。我試圖在Chrome 17.0.963.2和IE 8.0中運行代碼,並且沒有錯誤就可以正常工作。我使用的是Firefox 11.0圖像交換在Firefox中不起作用,但在Chrome和IE中可用
HTML
<body>
<div id = "first" class = "thumbnail">
<img class = "thumbsize" src = "22a.jpg" />
</div>
<div id = "second" class = "thumbnail">
<img class = "thumbsize" src = "22b.jpg" />
</div>
<input type = "button" id = "swap" value = "swap" />
</body>
JS
<script type = "text/javascript">
document.getElementById("swap").onclick = function(){
if(document.images){
var imgArray = document.images;
imgArray[2] = new Image();
imgArray[2].src = imgArray[0].src;
imgArray[0].src = imgArray[1].src;
imgArray[1].src = imgArray[2].src;
}
};
</script>
感謝啓蒙和代碼作品像魅力。 – Tifa