2014-02-14 53 views
2

我有元素的下一個結構:訪問容器(iframe內的iframe)

<div class="container"> 
    <iframe class="outer"> 
    .. 
     <iframe class="inner"> 
     actual markup with scripts here 
     </iframe> 
    </iframe> 
</div> 

使用純JavaScript,我已經meneged出去「內」的iframe到外之一:

//this is HTML tag of ".outer" iframe 
var parent = window.parent.document.getElementsByTagName('html')[0]; 

但我仍然需要到「.container」元素才能操作它。 任何人都可以告訴我如何從「.inner」元素內的腳本獲得「.container」元素??

回答

5

也許這:

var outer = window.parent; 

var mainWindow = outer.parent; 

var container = mainWindow.document.getElementsByClassName('container')[0]; 

,但不要忘了,你的I幀必須在同一個域。

+0

謝謝。現在玩跨域請求:) – Kamilius

1

我會嘗試

var containerElement = window.frames.top.top.document.getElementsByClassName('container')[0]; 

但要小心:如果你試圖擺脫你的框架,並希望與不同的域,端口或協議,您的瀏覽器(希望)訪問文檔會返回像這樣的例外:

Uncaught SecurityError: Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.

+0

因爲它發生。所以我使用easyxdm,爲了突破:) – Kamilius