HTML DOM Document querySelectorAll() method

Tirin da Koyarwa

querySelectorAll() Kauyi yauka dukkan iyali da su yantin da su dace da yantin CSS.

querySelectorAll() Kauyi yauka NodeList.

hadda yantin ba yin querySelectorAll() Kauyi yauka SYNTAX_ERR Yanfi.

Mutum

Anfani 1

Yantin dukkan class="example"

document.querySelectorAll(".example");

亲自试一试

Anfani 2

Dabbancin zaiwa <p>

const nodeList= document.querySelectorAll("p");
nodeList[0].style.backgroundColor = "red";

亲自试一试

Anfani 3

Dabbancin zaiwa <p class="example">

const nodeList = document.querySelectorAll("p.example");
nodeList[0].style.backgroundColor = "red";

亲自试一试

Anfani 4

Iyali class="example" na bayan

let numb = document.querySelectorAll(".example").length;

亲自试一试

Anfani 5

Dabbancin zaiwa dukkan class="example"

const nodeList = document.querySelectorAll(".example");
for (let i = 0; i < nodeList.length; i++) {
  nodeList[i].style.backgroundColor = "red";
}

亲自试一试

例子 6

设置所有

元素的背景颜色:

let nodeList = document.querySelectorAll("p");
for (let i = 0; i < nodeList.length; i++) {
  nodeList[i].style.backgroundColor = "red";
}

亲自试一试

例子 7

设置所有使用 "target" 属性的 元素的边框:

const nodeList = document.querySelectorAll("a[target]");
for (let i = 0; i < nodeList.length; i++) {
  nodeList[i].style.border = "10px solid red";
}

亲自试一试

例子 8

设置父元素是

元素的每个

元素的背景颜色:

const nodeList = document.querySelectorAll("div > p");
for (let i = 0; i < nodeList.length; i++) {
  nodeList[i].style.backgroundColor = "red";
}

亲自试一试

例子 9

设置所有

元素的背景颜色:

const nodeList = document.querySelectorAll("h3, div, span");
for (let i = 0; i < nodeList.length; i++) {
  nodeList[i].style.backgroundColor = "red";
}

亲自试一试

语法

document.querySelectorAll(CSSselectors)

参数

参数 Description
CSSselectors

必需。一个或多个 CSS 选择器。

CSS 选择器根据 id、类、类型、属性、属性值等选择 HTML 元素。

如需完整列表,请访问我们的 CSS selectors ref handbuk

对于多个选择器,请用逗号分隔每个选择器(请参阅上方的实例)。

Return value

Type Description
Object

Object na NodeList kowane da kowane kowane da kowane da kowane kowane kowane da kowane kowane.

Idan ana raba kowane, a yi NodeList na kowane.

Difference na HTMLCollection da NodeList

NodeList and HTMLcollection wannan kowa kusa da kusa

wanan kowa sune kuka dukiya (kukiru) na kananin kusa (kukiru) daga dukiya (kukiru) daga dukiya (kukiru). A hauka kananin kusa (kukiru) da kowane kowane da a cikin tashi (kukiru) da kowane kowane. Tashi daga 0 bace.

wanan kowa length 属性, ita a gida (kukiru) kananin raba (kukiru) na kowane.

HTMLCollection niDocument ElementCollection.

NodeList isDocument NodeCollection of (element nodes, attribute nodes, and text nodes).

HTMLCollection items can be accessed by their name, id, or index number.

NodeList items can only be accessed by their index number.

HTMLCollection is alwaysLiveCollection.

For example: If an <li> element is added to the list in the DOM, the list in the HTMLCollection will also change.

NodeList is usuallyStaticCollection.

For example: If an <li> element is added to the list in the DOM, the list in the NodeList will not change.

getElementsByClassName() and getElementsByTagName() Method returns a live HTMLCollection.

querySelectorAll() Method returns a static NodeList.

childNodes Property returns a live NodeList.

Browser Support

document.querySelectorAll() Is DOM Level 3 (2004) Feature.

All Browsers Support It:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support 9-11 Support Support Support Support