我试图循环使用forEach从qazxsw poi返回的所有元素。任何想法为什么这在FF,Chrome或IE中不起作用?
getElementsByTagName("input")
您需要将nodelist转换为数组:
<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>
或用于循环。
<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>
并将ShowResults函数更改为:
for(i = 0;i < input.length; i++)
{
ShowResults(input[i].value);
}
是的,ES6:
function ShowResults(value) {
alert(value);
}
const children = [...parent.getElementsByTagName('tag')];
children.forEach((child) => { /* Do something; */ });
因为MDN Doc for Spread Operator (...
)不是一个数组,所以它是input
使用HTMLCollection
循环会更好。
因为for
s是类似阵列的对象,你可以像这样HTMLCollection
call
Array#forEach
这是因为输入是html集合。 html集合没有forEach。
您可以通过Array.prototype.slice轻松将其转换为数组
例:
Array.prototype.forEach.call(input, ShowResults);
function ShowResults(value, index, ar) {
alert(index);
}
var input = document.getElementsByTagName("input");
alert(input.length);
input = Array.prototype.slice.call(input)
input.forEach(ShowResults);
原因是,这不起作用是因为'getElementsByTagName'返回一个数组 - 比如Object而不是实际的数组。如果您不知道,以下是两者的外观: -
http://jsfiddle.net/fPuKt/1/
因此,由于类似Array的对象继承自'Object.prototype'而不是'Array.prototype',这意味着类似Array的对象无法访问常见的Array原型方法,如forEach(),push(),map(), filter()和slice()。
希望有所帮助!
var realArray = ['a', 'b', 'c'];
var arrayLike = {
0: 'a',
1: 'b',
2: 'c',
length: 3
};
返回getElementsByTagName
,没有HTMLCollection
方法。但是,有一个简单的调整,允许您在不创建中间数组的情况下使用forEach
进行迭代:使用forEach
代替。 querySelectorAll
返回querySelectorAll
,现代浏览器有NodeList
方法:
NodeList.prototype.forEach
document.querySelectorAll('input')
.forEach((input) => {
console.log(input.value);
});
使用<input type="text" value="foo">
<input type="text" value="bar">
的另一个好处是它接受以逗号分隔的查询字符串,它比标记名称更灵活,更精确。例如,查询字符串
querySelectorAll
将匹配.container1 > span, .container2 > span
s,它们是元素的子元素与span
或container1
类:
container2
document.querySelectorAll('.container1 > span, .container2 > span')
.forEach((span) => {
span.classList.add('highlight');
});
.highlight {
background-color: yellow;
}
如果你想在没有内置方法的古老浏览器上使用<div class="container1">
<span>foo</span>
<span>bar</span>
</div>
<div class="container2">
<span>baz</span>
</div>
<div class="container3">
<span>buzz</span>
</div>
,只需添加一个NodeList.prototype.forEach
。以下代码片段适用于IE11:
polyfill
// Polyfill:
if (window.NodeList && !NodeList.prototype.forEach) {
NodeList.prototype.forEach = function(callback, thisArg) {
thisArg = thisArg || window;
for (var i = 0; i < this.length; i++) {
callback.call(thisArg, this[i], i, this);
}
};
}
// Main code:
document.querySelectorAll('.container1 > span, .container2 > span')
.forEach(function(span) {
span.classList.add('highlight');
});
.highlight {
background-color: yellow;
}
HTMLCollections与数组的方法不同。您可以在浏览器的javascript控制台中查看此内容。
<div class="container1">
<span>foo</span>
<span>bar</span>
</div>
<div class="container2">
<span>baz</span>
</div>
<div class="container3">
<span>buzz</span>
</div>
如果var elements = document.getElementsByClassName('some-class');
'forEach' in elements;
(在这种情况下)有一个名为true
的方法来调用,控制台将返回elements
。
在ES6中,您可以使用forEach
运算符将HtmlCollection转换为数组。看到这个问题spread
Why can't I use Array.forEach on a collection of Javascript elements?
我这样做了:
input = [...input]
input.forEach(ShowResults)
您现在可以在每个HTMLCollection.prototype.map = Array.prototype.map;
上使用地图。
HTMLCollection