开发者

Window/Document reference from jquery object

开发者 https://www.devze.com 2023-03-13 22:23 出处:网络
I have 2 functions like below: In popup window: function caller(){ window.opener.myfunc($(\'.TestDiv\'));开发者_运维技巧

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号