使用js给一个div下所有img标签添加点击事件,测试时点击后没有执行,浏览器报错TypeError: document.getElementById(...) is null,要怎要修改?
代码:
function showimg() {
document.getElementById("showImg").addEventListener("click", function() {
document.getElementById("showImg").className = 'hide';
document.getElementById("showImg").innerHTML = '';
});
var imgs = document.getElementById("content").getElementsByTagName("img");
for (var i = 0; i < imgs.length; i++) {
imgs[i].addEventListener("click", function() {
document.getElementById("showImg").className = 'show';
document.getElementById("showImg").innerHTML = '';
});
};
}
window.onload = showimg();
#showImg{position:absolute;position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(255,255,255,.9);z-index:99;}
#showImg img{position:absolute;top:0;left:0;right:0;bottom:0;margin:auto;max-height:90%;max-width:90%}
#showImg.hide{pointer-events:none;opacity:0;z-index:-1;}