您需要使用以下命令将节点列表转换为数组:
<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"); var inputList = Array.prototype.slice.call(input); alert(inputList.length); inputList.forEach(ShowResults); </script> </body></html>或用于循环。
for(i = 0;i < input.length; i++){ ShowResults(input[i].value);}并将ShowResults函数更改为:
function ShowResults(value) { alert(value);}为什么我们需要这样做?
Javascript中的某些对象看起来像一个数组,但不是一个。这通常意味着它们具有索引访问和length属性,但是没有任何数组方法。示例包括特殊的变量参数,DOM节点列表和字符串。类似于数组的对象和通用方法提供了使用类似数组的对象的技巧。
更新2019年7月10日
如今,您可以使用ES6
[...inputList].forEach或
Array.from(inputList)



