gpt4 book ai didi

javascript - querySelectorAll 返回什么类型的数据?

转载 作者:行者123 更新时间:2023-12-03 01:13:18 25 4
gpt4 key购买 nike

javaScript 对象没有 length 属性,但返回值 querySelectorAll有一个 length 属性,表明它是一个数组。但如果我们通过 Array.isArray() 检查它然后返回 false,证明它不是数组。那么它是什么类型的数据呢?

var obj1 = {
fname: "Mirajul",
lname: "Momin",
age: 24
};
console.log(obj1.length);
var paraList = document.querySelectorAll("p");
console.log(paraList.length);
console.log(Array.isArray(paraList));
<p>This is paragraph one</p>
<p>This is paragraph two</p>
<p>This is paragraph three</p>
<p>This is paragraph four</p>

最佳答案

The Element method querySelectorAll() returns a static (not live) NodeList representing a list of the document's elements that match the specified group of selectors.

有关差异,请访问:Difference between HTMLCollection, NodeLists, and arrays of objects

您可以使用Spread syntax将其作为数组:

var obj1 = {
fname: "Mirajul",
lname: "Momin",
age: 24
};
console.log(obj1.length);
var paraList = [...document.querySelectorAll("p")];
console.log(paraList.length);
console.log(Array.isArray(paraList));
<p>This is paragraph one</p>
<p>This is paragraph two</p>
<p>This is paragraph three</p>
<p>This is paragraph four</p>

关于javascript - querySelectorAll 返回什么类型的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52128519/

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