我試圖用javascript和for
循環將一系列圖像添加到html頁面。但是當我測試頁面時,事件監聽器如onclick
和onmouseover
沒有響應。使用javascript添加onclick事件監聽器和元素
function changeText(element)
{
var menuItems = ['menu_item1','menu_item2','menu_item3','menu_item4'];
var galleryImages = ["s1.jpg","s2.jpg","s3.jpg","s4.jpg","s5.jpg","s6.jpg","s7.jpg","s8.jpg","s9.jpg","s10.jpg","s11.jpg","s12.jpg","s13.jpg","s14.jpg","s15.jpg","s16.jpg","s17.jpg","s18.jpg","s19.jpg","s20.jpg"];
var itemText = document.getElementsByClassName(element);
var textString ="";
if(menuItem == menuItems[3])
{
for(var i=0;i<galleryImages.length;i++){
textString+="<img id='img"+i+"'class='imageGallery'src='galleryImgs/s"+i+".jpg' width='150px' onclick='imageZoom('img"+i+"')'>";
}
itemText[0].innerHTML = 'This is now menu_item4 text Left side';
itemText[1].innerHTML = textString
}
return;
}
function imageZoom(img)
{
var image = document.getElementById(img);
image.style.width = "400px";
}
您是否在javascript控制檯中發生錯誤? –
我懷疑如果你檢查控制檯,你會看到像'imageZoom沒有定義'的錯誤。 –