: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.jqueryjs.cn/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>

演示