如何使用JavaScript循环遍历数组中的所有条目?
我以为它是这样的:
forEach(instance in theArray)
Run Code Online (Sandbox Code Playgroud)
theArray
我的阵列在哪里,但这似乎是不正确的.
我正在编写一个简短的脚本来更改<abbr>
元素的内部文本,但发现它nodelist
没有forEach
方法.我知道这nodelist
不会继承Array
,但似乎不是forEach
一个有用的方法吗?是否有一个具体的实施问题,我没有意识到阻止添加forEach
到nodelist
?
注意:我知道Dojo和jQuery都有forEach
某种形式的节点列表.由于限制我无法使用.
我试图遍历getElementsByTagName("input")
使用forEach 重新编译的所有元素.任何想法为什么这在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>
Run Code Online (Sandbox Code Playgroud)