欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript?DOM?querySelectorAll()?使用方法

 更新時間:2023年06月11日 10:24:15   作者:runoob  
querySelectorAll()?方法返回文檔中匹配指定?CSS?選擇器的所有元素,返回?NodeList?對象,一般用來獲取指定id火class下的所有節(jié)點

querySelectorAll定義與用法

querySelectorAll() 方法返回文檔中匹配指定 CSS 選擇器的所有元素,返回 NodeList 對象。

NodeList 對象表示節(jié)點的集合??梢酝ㄟ^索引訪問,索引值從 0 開始。

提示: 你可以使用 NodeList 對象的 length 屬性來獲取匹配選擇器的元素屬性,然后你可以遍歷所有元素,從而獲取你想要的信息。

瀏覽器支持

表格中的數(shù)字表示支持該方法的第一個瀏覽器的版本號。

表格中的數(shù)字表示支持該方法的第一個瀏覽器的版本號。

方法ChromeEdgeFirefoxSafariOpera
querySelectorAll()4.08.03.53.210.0

注意: Internet Explorer 8 支持 CSS2 選擇器。 IE9 及更高版本的瀏覽器已經(jīng)支持 CSS3 選擇器。

語法

elementList = document.querySelectorAll(selectors);
  • elementList 是一個靜態(tài)的 NodeList 類型的對象。
  • selectors 是一個由逗號連接的包含一個或多個 CSS 選擇器的字符串。

屬性值

參數(shù)類型描述
CSS 選擇器String必須。 指定一個或多個匹配 CSS 選擇器的元素??梢酝ㄟ^ id, class, 類型, 屬性, 屬性值等作為選擇器來獲取元素。

多個選擇器使用逗號(,)分隔。

提示: CSS 選擇器更多內(nèi)容可以參考 CSS 選擇器參考手冊。

方法

DOM 版本:Level 1 Document Object
返回值:一個 NodeList 對象,表示文檔中匹配指定 CSS 選擇器的所有元素。 NodeList 是一個靜態(tài)的 NodeList 類型的對象。如果指定的選擇器不合法,則拋出一個 SYNTAX_ERR 異常。

實例

為 class="example" (索引為 0) 的第一個元素設(shè)置背景顏色

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>腳本之家</title>
</head>
<body>
<h2 class="example">使用 class="example" 的標(biāo)題</h2>
<p class="example">使用 class="example" 的段落</p> 
<p>點擊按鈕為 class="example" (索引為 0) 的第一個元素設(shè)置背景顏色。</p>
<button onclick="myFunction()">點我</button>
<p><strong>注意:</strong>Internet Explorer 8  及更早版本不支持 querySelectorAll() 方法。</p>
<script>
function myFunction() {
    var x = document.querySelectorAll(".example");
    x[0].style.backgroundColor = "red";
}
</script>
</body>
</html>

獲取文檔中所有的 <p> 元素, 并為匹配的第一個 <p> 元素 (索引為 0) 設(shè)置背景顏色:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>querySelectorAll 腳本之家(jb51.net)</title>
</head>
<body>
<p>這是一個 p 元素。</p>
<p>這也是一個 p 元素。</p>
<p>點擊按鈕為文檔中第一個 p (索引為 0) 元素設(shè)置背景顏色。</p>
<button onclick="myFunction()">點我</button>
<p><strong>注意:</strong>Internet Explorer 8  及更早版本不支持 querySelectorAll() 方法。</p>
<script>
function myFunction() {
    var x = document.querySelectorAll("p");
    x[0].style.backgroundColor = "red";
}
</script>
</body>
</html>

獲取文檔中所有 class="example" 的 <p> 元素, 并為匹配的第一個 <p> 元素 (索引為 0) 設(shè)置背景顏色:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>querySelectorAll 腳本之家(jb51.net)</title>
</head>
<body>
<h2 class="example">使用 class="example" 的標(biāo)題</h2>
<p class="example">使用 class="example" 的段落</p> 
<p class="example">另外一個使用 class="example" 的段落</p> 
<p>點擊按鈕為第一個 class="example" (索引為 0) 的 p 元素設(shè)置背景顏色。</p>
<button onclick="myFunction()">點我</button>
<p><strong>注意:</strong>Internet Explorer 8  及更早版本不支持 querySelectorAll() 方法。</p>
<script>
function myFunction() {
    var x = document.querySelectorAll("p.example");
    x[0].style.backgroundColor = "red";
}
</script>
</body>
</html>

計算文檔中 class="example" 的 <p> 元素的數(shù)量(使用 NodeList 對象的 length 屬性):

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>querySelectorAll 腳本之家(jb51.net)</title>
</head>
<body>
<div class="example">
使用 class="example" 的 div 元素
</div>
<div class="example">
另一個使用 class="example" 的 div 元素
</div>
<p class="example">使用 class="example" 的 p 元素</p>
<p>點擊按鈕計算 class="example" 的元素的數(shù)量。</p>
<button onclick="myFunction()">點我</button>
<p><strong>注意:</strong>Internet Explorer 8  及更早版本不支持 querySelectorAll() 方法。</p>
<p id="demo"></p>
<script>
function myFunction() {
    var x = document.querySelectorAll(".example");
    document.getElementById("demo").innerHTML = x.length;
}
</script>
</body>
</html>

設(shè)置文檔中所有 class="example" 元素的背景顏色:

var x = document.querySelectorAll(".example");
var i;
for (i = 0; i < x.length; i++) {
? ? x[i].style.backgroundColor = "red";
}

完整實例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>querySelectorAll 腳本之家(jb51.net)</title>
<style>
.example {
    border: 1px solid black;
    margin: 5px;
}
</style>
</head>
<body>
<div class="example">
使用 class="example" 的 div
</div>
<div class="example">
另外一個使用 class="example" 的 div
</div>
<p class="example">使用 class="example" 的 p 元素</p>
<p>這是一個使用了 class="example" 的 <span class="example">span</span> 元素,它在 p 元素里面。</p>
<p>點擊按鈕修改所有 class="example" 元素的背景顏色。</p>
<button onclick="myFunction()">點我</button>
<p><strong>注意:</strong>Internet Explorer 8  及更早版本不支持 querySelectorAll() 方法。</p>
<p id="demo"></p>
<script>
function myFunction() {
    var x = document.querySelectorAll(".example");
    var i;
    for (i = 0; i < x.length; i++) {
        x[i].style.backgroundColor = "red";
    }
}
</script>
</body>
</html>

設(shè)置文檔中所有 <p> 元素的背景顏色:

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

完整實例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>querySelectorAll 腳本之家(jb51.net)</title>
<style>
.example {
    border: 1px solid black;
    margin: 5px;
}
</style>
</head>
<body>
<h1>一個 h1 元素</h1>
<div>一個 div 元素</div>
<p>一個 p 元素。</p>
<p>另一個 p 元素。</p>
<div class="example">
  <p>在 div 中的一個 p 元素。</p>
</div>
<p>點擊按鈕修改文檔中所有 p 元素的背景顏色。</p>
<button onclick="myFunction()">點我</button>
<p><strong>注意:</strong>Internet Explorer 8  及更早版本不支持 querySelectorAll() 方法。</p>
<script>
function myFunction() {
    var x = document.querySelectorAll("p");
    var i;
    for (i = 0; i < x.length; i++) {
        x[i].style.backgroundColor = "red";
    }
}
</script>
</body>
</html>

查找文檔中共包含 "target" 屬性的 <a> 標(biāo)簽,并為其設(shè)置邊框:

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

完整實例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>querySelectorAll 腳本之家(jb51.net)</title>
<style>
a[target] {
    background-color: yellow;
}
</style>
</head>
<body>
<p> CSS 選擇器 a[target] 確保所有包含有  target 屬性的 a 標(biāo)簽都設(shè)置背景顏色。</p>
<a  rel="external nofollow" >菜鳥教程</a>
<a  rel="external nofollow"  target="_blank">Google</a>
<a  rel="external nofollow"  target="_top">wikipedia.org</a>
<p>點擊按鈕為所有包含 target 屬性的 a 標(biāo)簽設(shè)置邊框。</p>
<button onclick="myFunction()">點我</button>
<p><strong>注意:</strong>Internet Explorer 8  及更早版本不支持 querySelectorAll() 方法。</p>
<script>
function myFunction() {
    var x = document.querySelectorAll("a[target]");
    var i;
    for (i = 0; i < x.length; i++) {
        x[i].style.border = "10px solid red";
    }
}
</script>
</body>
</html>

查找每個父元素為 <div> 的 <p> 元素,并為其設(shè)置背景顏色:

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

完整實例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>querySelectorAll 腳本之家(jb51.net)</title>
<style>
div {
    border: 1px solid black;
    margin-bottom: 10px;
}
</style>
</head>
<body>
<div>
  <h3>H3 元素</h3>
  <p>我是一個 p 元素,我的父元素是 div 元素。</p>
</div>
<div>
  <h3>H3 元素</h3>
  <p>我是一個 p 元素,我的父元素也是 div 元素。</p>
</div>
<p>點擊按鈕修改父元素為 div 的所有 p 元素的背景顏色。</p>
<button onclick="myFunction()">點我</button>
<p><strong>注意:</strong>Internet Explorer 8  及更早版本不支持 querySelectorAll() 方法。</p>
<script>
function myFunction() {
    var x = document.querySelectorAll("div > p");
    var i;
    for (i = 0; i < x.length; i++) {
        x[i].style.backgroundColor = "red";
    }
}
</script>
</body>
</html>

給文檔中所有的 <h2>, <div> 和 <span> 元素設(shè)置背景顏色:

var x = document.querySelectorAll("h2, div, span");
var i;
for (i = 0; i < x.length; i++) {
? ? x[i].style.backgroundColor = "red";
}

完整實例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>querySelectorAll 腳本之家(jb51.net)</title>
</head>
<body>
<h1>一個 H1 元素</h1>
<h2>一個 H2 元素</h2>
<div>一個 DIV 元素</div>
<p>一個 p 元素</p>
<p>一個 p 元素,包含了 <span style="color:brown;">span</span> 元素了。 </p>
<p>點擊按鈕設(shè)置使用 h2, div 和 span 元素的背景顏色。</p>
<button onclick="myFunction()">點我</button>
<p><strong>注意:</strong>Internet Explorer 8  及更早版本不支持 querySelectorAll() 方法。</p>
<script>
function myFunction() {
    var x = document.querySelectorAll("h2, div, span");
    var i;
    for (i = 0; i < x.length; i++) {
        x[i].style.backgroundColor = "red";
    }
}
</script>
</body>
</html>

到此這篇關(guān)于javascript DOM querySelectorAll() 使用方法的文章就介紹到這了,更多相關(guān)js querySelectorAll內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論