object = ["a","input","textarea","select","img","#content",".view"];
for (var i = 0; i < oj.length; i++) {
var resources = document.querySelector(object[i]);
resources.addEventListener('mouseover', function(){alert("test");}, false);
}
错误TypeError: resources is null
,如何解决?
答案 0 :(得分:3)
我想基本检查非虚假值(元素匹配选择器)是要走的路:
if (resources) {
resources.addEventListener("mouseover", function() { ... }, false);
}
答案 1 :(得分:1)
没有与数组中的某个选择器匹配的元素。在附加事件之前,请进行简单检查以确保其未定义。
var object = ["a","input","textarea","select","img","#content",".view"];
for (var i = 0; i < oj.length; i++) {
var resources = document.querySelector(object[i]);
if(resources){
resources.addEventListener('mouseover', function(){alert("test");}, false);
}
}