【发布时间】:2013-10-19 22:06:18
【问题描述】:
我正在尝试使用 forEach 遍历从 getElementsByTagName("input") 重新提取的所有元素。任何想法为什么这在 FF、Chrome 或 IE 中不起作用?
<html>
<head>
</head>
<body>
<input type="text" value="" />
<input type="text" value="" />
<script>
function ShowResults(value, index, ar) {
alert(index);
}
var input = document.getElementsByTagName("input");
alert(input.length);
input.forEach(ShowResults);
</script>
</body>
</html>
【问题讨论】:
-
为什么没有
forEach: stackoverflow.com/questions/13433799/…的理由 -
现在在 ES6 NodeList 中有
forEach,但 HTMLCollection 仍然没有。不幸的是,getElementsByTagName返回 HTMLCollection。考虑使用querySelectorAll -
有错误提示吗?
标签: javascript arrays foreach getelementsbytagname