:root 選擇器


root 選擇器

說明: 選取文件根目錄的元素。

  • 新增版本: 1.9jQuery( ":root" )

在 HTML 中,文件的根目錄,也就是 $(":root") 選取的元素,永遠是 <html> 元素。

範例

顯示根元素的標籤名稱。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>root demo</title>
<style>
span.fot {
color: red;
font-size: 120%;
font-style: italic;
}
</style>
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
</head>
<body>
<div id="log">The root of this document is: </div>
<script>
$( "<b></b>" ).html( $( ":root" )[ 0 ].nodeName ).appendTo( "#log" );
</script>
</body>
</html>

示範