for
我正在尝试使用 javascript 和循环将一系列图像添加到 html 页面。但是当我测试页面时,事件侦听器(例如onclick
和onmouseover
)没有响应。
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";
}