简体   繁体   English

HTMLCollection长度与记录的对象不匹配

[英]HTMLCollection Length does not match logged object

// Find all posts
var posts = node.getElementsByClassName("userContentWrapper");
var post, text;
console.log(posts);
console.log(typeof(posts));
console.log(Object.keys(posts));
console.log(posts.length);

The above outputs: 以上输出: 在此处输入图片说明

:( I don't understand. Why is length 0? It works in a fiddle: http://jsfiddle.net/p7yfv37s/ :(我不明白。为什么长度为0?它在小提琴中起作用: http : //jsfiddle.net/p7yfv37s/

I also printed the object again after the length, and it still shows length: 4. 我还在长度之后再次打印了该对象,它仍然显示长度:4。 在此处输入图片说明

If you put your cursor over the i icon you will see that HtmlCollection state is captured only upon first expansion. 如果将光标放在i图标上,您将看到仅在第一次扩展时捕获HtmlCollection状态。 It does not show its state at the moment of console.log invocation. console.log调用时,它不会显示其状态。

在此处输入图片说明

Most likely logging is performed before DOM is loaded or node is dynamically populated by javascript. 最有可能的日志记录是在加载DOM或用JavaScript动态填充node之前执行的。 HTMLCollection is kind of live collection which track all DOM changes ( more info about difference between HTMLCollection and NodeList ) HTMLCollection是一种实时收集,可跟踪所有DOM更改( 有关HTMLCollectionNodeList之间的HTMLCollection更多信息

See simple example: http://jsfiddle.net/p7yfv37s/1/ 参见简单示例: http : //jsfiddle.net/p7yfv37s/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM