querySelectorAll返回什么类型的数据? [英] What type of data does querySelectorAll return?

查看:87
本文介绍了querySelectorAll返回什么类型的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

A javaScript object does not have a length property, but the returned value of querySelectorAll has a length property, indicating that it's an array. But if we check it by Array.isArray() then it returns false, proving that it is not an array. What type of data is it then?

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>

推荐答案

Element方法 querySelectorAll() 返回一个静态(非实时)NodeList,该NodeList表示与指定的选择器组匹配的文档元素的列表.

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.

有关差异,请访问: HTMLCollection,NodeList和对象数组

您可以使用扩展语法使其成为数组:

You can use Spread syntax to make that as an array:

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>

这篇关于querySelectorAll返回什么类型的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆