我試圖創建一個簡單的圖庫使用JavaScript,但每次我點擊縮略圖,screen
或最大的圖片(我的意思是主圖片)沒有改變。無法獲得目標元素
<div id='headImage'>
<img src='bron.jpg' id='screen' height='300' width='400'/>
</div>
<div id='imagelist' onclick='changeImage(event)'>
<img src='bron.jpg' height='150' width='150'/>
<img src='curry2.jpg' height='150' width='150'/>
<img src='irving.jpg' height='150' width='150'/>
<img src='phelps.jpg' height='150' width='150'/>
<img src='stonie.jpg' height='150' width='150'/>
</div>
<script type='text/javascript'>
function changeImage(event)
{
event = event || window.event;
var targetElement = event.target || event.srcElement;
//if(targetElement ='IMG'){
document.getElementById("screen").src = targetElement.getAttribute("src");
//}
}
</script>
你的功能叫做changeImage,但是你的ay onclick =「cange()」。將onclick更改爲:'onclick =「changeImage();」' –