2015-10-15 68 views
1

我的網頁由2 frame s組成,其中nameid屬性設置爲「左」和「中」。從兄弟框架中找到包含iframe的框架

我運行在左一個JS腳本能夠定位/在中央框架識別iframenameid「monfrm」。

這裏是JavaScript我到目前爲止:

x = top.document.getElementById("center"); 
alert(x.src); //fine 
// ok up to here 
y = x.getElementById("monfrm"); // attempting to locate my iframe within "center" frame & that's where it hurts 
alert("left says" + y.width); //eeh nothing 

我可能老了COS'似乎容易得可憐,除非我錯過了點.... :-)

任何光棚歡迎

感謝

+0

是'width'有效的HTML元素屬性?也許嘗試'offsetWidth'可以解決你的問題。 – Federico

+0

看在控制檯上。你會有一個關於getElementById的錯誤 –

回答

1

如果你想使用getElementByIdframe/iframe內選擇,你需要CA LL它的框架的contentWindow.document如圖here

document.getElementById('myframe1').contentWindow.document.getElementById('x') 

鑑於你的代碼,你想設置

y = x.contentWindow.document.getElementById("monfrm"); 
+0

你們真是太棒了,這兩個評論都讓我解決了這個問題,非常感謝 – Majax

+0

如果你的問題已經得到解答,請考慮將答案標記爲已接受(左邊的複選標記),以便問題可以被關閉。 :) – arcyqwerty