2011-05-16 62 views
38

有沒有辦法在父窗口中使用jQuery選擇DIV?jQuery在父窗口中選擇元素

例如:

主頁包含此,

<div id="testdiv"></div> 

彈出頁面有一些選項和「應用」按鈕的形式。當用戶點擊應用時,它會影響主頁面上的樣式屬性。

東西沿的邏輯,

parent.$("#testdiv").attr("style", content from form); 
+0

[如何使用jquery訪問父窗口對象?](http://stackoverflow.com/questions/2167455/how-to-access-parent-window-object-using-jquery) – 2015-06-16 16:00:10

回答

94

使用上下文參數

$("#testdiv",parent.document) 

但如果你真的使用彈出,你需要訪問首戰代替

$("#testdiv",opener.document) 
+0

完美謝謝。 – Rob 2011-05-16 13:11:26

+0

今天學到了新東西,謝謝:) – Manu 2016-09-15 09:46:08

14

我尋找解決這個問題的方法,並且遇到了本頁面。我實施了上述解決方案:

$("#testdiv",opener.document) //doesn't work 

但它不起作用。也許它在以前的jQuery版本中工作過,但現在看起來不起作用。

我發現了另一個計算器頁面上這個工作液: how to access parent window object using jquery?

從中我得到這個工作的解決方案:

window.opener.$("#testdiv") //This works. 
+1

非常棒的隊友! @Molle博士過度自信! – 2013-11-17 06:56:46

+0

..或它實際上在2011年工作。對於這個答案的解決方案工作,該彈出窗口是否需要已經加載jQuery? – Dinerdo 2017-07-27 15:55:04

0

爲什麼不能既要肯定嗎?

if(opener.document){ 
    $("#testdiv",opener.document).doStuff(); 
}else{ 
    $("#testdiv",window.opener).doStuff(); 
} 
2

你也可以使用,

parent.jQuery("#testdiv").attr("style", content from form);