使用 jquery 选择文档根
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11666385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
select document root using jquery
提问by tomsv
I can select the body and html parts of the document using
我可以选择文档的正文和 html 部分使用
$('body')
and
和
$('html')
respectively, but how do I select the document root?
分别,但我如何选择文档根?
回答by adeneo
Not sure what you mean, but to select the document you do
不确定你的意思,但要选择你做的文件
$(document);
To get the contents of the document I'm guessing you need the documentElement, which is just the same as the <html>
tag in most enviroments.
要获取文档的内容,我猜您需要 documentElement,它与<html>
大多数环境中的标签相同。
$(document.documentElement);
回答by GetFree
The root of the DOM is always the html
element.
You can get it either with $('html')
or $(':root')
.
DOM 的根始终是html
元素。
您可以使用$('html')
或获取它$(':root')
。
The following assertions should always be true:
以下断言应始终为真:
$('html')[0] === $(':root')[0]
$(':root')[0] === document.documentElement
回答by Alok Jain
The Document interface inherits from Node, and represents the whole document, such as an HTML page. Although the Document node is conceptually the root of a document, it isn't physically the root - the root node is the first Element node in the Document, and is represented by its documentElement property.
Document 接口继承自 Node,代表整个文档,比如一个 HTML 页面。尽管 Document 节点在概念上是文档的根,但它在物理上并不是根——根节点是文档中的第一个 Element 节点,并由其 documentElement 属性表示。
You can select documentElement with following code:
您可以使用以下代码选择 documentElement:
var root = document.documentElement;
OR
或者
$(document.documentElement);