:header 選擇器


header 選擇器

說明: 選取所有標頭元素,例如 h1、h2、h3 等。

  • 版本新增: 1.2jQuery( ":header" )

其他注意事項

  • 由於 :header 是 jQuery 擴充,不屬於 CSS 規範的一部分,因此使用 :header 的查詢無法利用原生 DOM querySelectorAll() 方法提供的效能提升。若要使用 :header 選取元素時獲得最佳效能,請先使用純 CSS 選擇器選取元素,然後使用 .filter(":header")

範例

將背景和文字顏色新增至頁面上的所有標頭。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>header demo</title>
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
</head>
<body>
<h1>Header 1</h1>
<p>Contents 1</p>
<h2>Header 2</h2>
<p>Contents 2</p>
<script>
$( ":header" ).css({ background: "#ccc", color: "blue" });
</script>
</body>
</html>

示範