admin管理员组文章数量:1287941
I am using:
var links = document.getElementsByTagName('a');
to get all links on the page. We need to modify a few.
if I console log links, I get an HTMLCollection of 100+ a elements. If I console log links.length, it's 0.
I've tried everything I can find to convert this to an array but nothing is working.
Array.from(links)
Array.prototype.slice.call(links)
[].forEach.call(links, function (el) {...});
HTMLCollection.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
HTMLCollection.prototype.forEach = Array.prototype.forEach;
These all produce an empty array.
Additionally var links = document.querySelectorAll('a');
produces an empty NodeList.
I've run out of options. The initial links variable is very much not empty. So I don't understand why all of these suggested options don't work. Additionally, jQuery is not an option for us.
I am using:
var links = document.getElementsByTagName('a');
to get all links on the page. We need to modify a few.
if I console log links, I get an HTMLCollection of 100+ a elements. If I console log links.length, it's 0.
I've tried everything I can find to convert this to an array but nothing is working.
Array.from(links)
Array.prototype.slice.call(links)
[].forEach.call(links, function (el) {...});
HTMLCollection.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
HTMLCollection.prototype.forEach = Array.prototype.forEach;
These all produce an empty array.
Additionally var links = document.querySelectorAll('a');
produces an empty NodeList.
I've run out of options. The initial links variable is very much not empty. So I don't understand why all of these suggested options don't work. Additionally, jQuery is not an option for us.
Share Improve this question asked Jan 23, 2018 at 19:57 pinksharpiipinksharpii 5271 gold badge8 silver badges19 bronze badges 2- 1 Can you reproduce the issue at stacksnippets? – guest271314 Commented Jan 23, 2018 at 20:00
- 1 All of these work fine. Your problem is elsewhere. – Jordan Running Commented Jan 23, 2018 at 20:10
4 Answers
Reset to default 6Array.from(HTML_COLLECTION).forEach(function (element) {
console.log(element);
});
this works for me
var links = document.getElementsByTagName('a');
var result = [].slice.call(links)
You could also use an EventListener
window.addEventListener('load', function () {
var links = document.getElementsByTagName('a');
console.log(links);
}
O
Almost had it: var linksAsArray = Array.prototype.slice.call(links, 0)
Also, as an alternative, you can write a helper function to avoid needing to convert nodelists to arrays all of the time. For example,
function each(elems, fn){
var i = -1;
while(++i < elems.length){
fn(elems[i])
}
}
each(links, function(link){ console.log(link); })
本文标签: arraysHow to iterate through HTMLCollection in JavascriptStack Overflow
版权声明:本文标题:arrays - How to iterate through HTMLCollection in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741313847a2371800.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论