2010-01-27 26 views
5

讓我...我已經提到這個問題/答案,它似乎包含線索前言本,但我仍然失蹤的全貌iframe中的框架的jQuery/javascript上下文是什麼?

Run JQuery in the context of another frame

本質上,索引頁的結構然後這個

<html> 
<body> 
    <div class="frames-wrap"> 
     <iframe id="this-iframe" src="location.php"> 

     </iframe> 
    </div> 
</body> 
</html> 

location.php包含有設置了像這樣的兩幀一個框架(啊哈,不是我的主意...)...

<frameset cols="350,*"> 
    <frame src="search.php" id="frame_search" name="search"/> 
    <frame src="edit.php" id="frame_edit" name="edit" /> 
</frameset> 

如果我想操縱索引頁面和這些元素之間的對象,我將如何去做這件事?

我一直在想上下文應該類似於window.parent.frames[0].document ......我還缺少什麼?

+0

也引用了這個,但我太新,不能發佈多個鏈接 http://stackoverflow.com/questions/997986/write-elements-into-a-child-iframe-using-javascript-or- jQuery的 – technicolorenvy 2010-01-27 21:22:38

+0

這個問題可能會幫助你:http://stackoverflow.com/questions/251420/invoking-javascript-in-iframe-from-parent-page(無恥的自我推銷,因爲問題最初是由我問) – 2010-01-27 21:33:50

回答

2

前言:除非源自相同的域,否則您將無法訪問iframe內容。

要選擇你的iframe元素,你可以使用一個jQuery調用這樣

element = $("#this_iframe").contents().find("#frame_search") 

的關鍵是使用contents()功能。請參閱Traversing/contents

+1

ahh contents()確實很棒,我沒有意識到提供了iframe支持......謝謝! – technicolorenvy 2010-01-28 18:43:54

2

我認爲從technicolorenvy的鏈接有答案,但選擇器有一個鮮爲人知的第二個參數,您可以設置上下文。

事情是這樣的:

var iframeDoc = document.getElementById('myIframe'); 
iframeDoc = (iframeDoc.contentWindow) ? iframeDoc.contentWindow : (iframeDoc.contentDocument.document) ? iframeDoc.contentDocument.document : iframeDoc.contentDocument; 


// From the parent window 
$('p', iframeDoc).html('Hello from parent'); 

http://docs.jquery.com/Core/jQuery#expressioncontext

1

給你的框架IDS是有效的JavaScript標識符會有所幫助,那麼你可以使用結構,如window.top.this_iframe.frame_edit.document作爲你的背景。

+0

謝謝你反饋!然而,這是對我的轟炸。這些ID現在有下劃線而不是ndashes。如果我使用console.log來查看「幕後」發生了什麼,它會返回一些東西,如果我登錄window.top.this_iframe.document或者如果我登錄window.top.document.getElementById('this_iframe'),看起來frame_edit ID沒有被「看見」。有任何想法嗎? – technicolorenvy 2010-01-27 23:52:25

0

這些都很有幫助。當我試圖通過DOM中的iframe時,我一直在轟炸。這看起來是因爲我的代碼駐留在ready()方法中,但在iframe中調用的框架集沒有被$(document).ready()引發的時間加載。

感謝您的所有幫助和反饋!