简体   繁体   中英

Window/Document reference from jquery object

I have 2 functions like below:

In popup window:

 function caller(){
    window.opener.myfunc($('.TestDiv'));
 }

In opener:

 function myfunc(element){
    alert(element.parents('html').html());
 }

The above gives me access to the html root element within myfunc.

How do I get access to the window or document object from the passed in element?

Note: it must be from the passed in element as this may be coming from a different context to that in which myfunc is running.

You can obtain the document node that contains the selected element with the ownerDocument property of the element node.

function myfunc(element) {
    element[0].ownerDocument;
}

[0] gets the native DOM element; ownerDocument gets the ancestor document node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM