2011-07-07 24 views
2

再次使用colorbox與iframe,我試圖將信息從colorbox傳回父窗口。所有我試圖做的是在顏色框關閉後更新帶有通過顏色框傳遞的新文本的div。jquery,試圖從colorbox更新div到父

JQuery(window.parent.document).find("#textdiv").html("Some updated text"); 
parent.jQuery.fn.colorbox.close(); 

或者

$(window.parent.document).find("#textdiv").html("Some updated text"); 
parent.jQuery.fn.colorbox.close(); 

或者

parent.JQuery("#textdiv").html("Some updated text"); 
parent.jQuery.fn.colorbox.close(); 

或者

parent.$("#textdiv").html("Some updated text"); 
parent.jQuery.fn.colorbox.close(); 

然後在父窗口我:我在顏色框彈出試過這種

<div id="textdiv"></div> 

jQuery(".purchasepop").colorbox({width:"80%", height:"80%", iframe:true, 
onClosed:function(){ alert($("#textdiv").html());return false; }}); 

但是無論我嘗試什麼,警告框都是空白的。有什麼建議麼?

回答

4

我玩了這一點,並有類似的困難。但是,我確定了一種解決方法。在父頁面中調用方法非常簡單。所以,在父母有:

function updateFunction(value) { 
    $("#textdiv").html(value); 
    $.colorbox.close(); // Or whatever - I don't know about colorbox 
} 

,並在,叫

parent.updateFunction("Some updated text"); 

這對我行之有效。我相信問題在於jQuery對象的範圍。你有兩個人(一個在父母身上,一個在iframe中),並且抓住他們會很煩人。我知道有一個很好的方法可以做到,但我現在不記得了。

更新:第二次嘗試,parent.$("#textdiv").html("Some Updated Text");工作得很好。所以,我的猜測是你的回調訂單有問題,或者parent不知何故被重新定義或指向其他地方。不過,上述策略也可能對此有所幫助。