当我运行以下代码时,萤火虫控制台说 elem 是未定义的,虽然......它不是!
var domTools = {};
domTools.searchInElements = function (elem, pattern) {
if (pattern.constructor !== RegExp) {
throw "Pattern must be a RegExp";
}
if (elem.constructor !== String) {
throw "Element must be a String";
}
elem = document.getElementsByTagName[elem];
var matches = [];
for (e = 0; e < elem.length; e++) {
if (pattern.test(elem[e].innerHTML)) {
matches.push(elem[e]);
}
}
return matches;
}
domTools.searchInElements("p", /hello/);
它在for
声明期间给了我错误。所有这些代码都在页面已经加载时运行。为什么会这样?