2016-09-02 24 views
1

假設你有下面的HTML中的元素獲取父iframe元素:由位於一個iframe

<div> 
    <iframe src=".."> 
     <!-- The following would be the content of the iframe --> 
     <html><head></head><body> 
      <span>I'm the node</span> 
     </body></html> 
    </iframe> 
</div> 

而且你必須包含iframe中的<span>元素的變量iframeNode。您的腳本上下文是父窗口。

你將如何得到父iframe元素:

<iframe src="..."></iframe> 

這個節點?

在理論上:

var iframe = iframeNode.myParentIframeElementInTheParentScope; 

回答

1

如果與iframe唯一引用是在iframe(= iframeNode)一個span,然後

var iframe = iframeNode.ownerDocument.defaultView.frameElement;

將指的IFRAME元素在父窗口。