javascript 从子元素获取文档对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4158268/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 10:33:08  来源:igfitidea点击:

Get document object from a child element

javascriptjquerydom

提问by Ozgur

Let's assume I have a jQuery object of a DIV element in body. I'd like to obtain document object by traversing. Can it be possible ?

假设我在 body 中有一个 DIV 元素的 jQuery 对象。我想通过遍历获取文档对象。有可能吗?

Note: window.document is not a option in my case.

注意:在我的情况下 window.document 不是一个选项。

Thank you.

谢谢你。

回答by casablanca

element.ownerDocumentwill give you a reference to the document to which any DOM element belongs.

element.ownerDocument将为您提供对任何 DOM 元素所属的文档的引用。

回答by palswim

Yes, the documentobject is the parent of the <HTML>element (at least in Firefox). Find it like this:

是的,document对象是<HTML>元素的父元素(至少在 Firefox 中)。像这样找到它:

function FindDoc(el) {
    while(el.parentNode) {
        el = el.parentNode;
    }
    return el;
}

回答by Andrew Collins

firstElementChild The first child that is an element node Traversal module.

firstElementChild 第一个子元素节点 遍历模块。