gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:58:53 24 4
gpt4 key购买 nike

// 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);

以上输出: enter image description here

:( 我不明白。为什么长度是 0?它在 fiddle 中工作: http://jsfiddle.net/p7yfv37s/

我还在长度之后再次打印了该对象,它仍然显示长度:4。enter image description here

最佳答案

如果将光标放在 i 图标上,您将看到仅在第一次展开时才捕获 HtmlCollection 状态。它不会显示调用 console.log 时的状态。

enter image description here

最有可能的日志记录是在加载 DOM 或通过 javascript 动态填充 node 之前执行的。 HTMLCollection 是一种实时集合,它跟踪所有 DOM 更改 ( more info about difference between HTMLCollection and NodeList )

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

关于javascript - HTMLCollection 长度与记录的对象不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30648522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com