2010-09-03 69 views
0

我使用的使用iframe讓用戶知道有會議即將到期jQuery的對話框中的iframe元素的引用。我想更新該對話框每秒鐘有倒計時,因此,我想緩存倒計時元素的引用,所以我們不必查詢DOM的它每秒。緩存使用jQuery

我試圖做到這一點許多不同的方式,我似乎無法得到它的工作。

$(documnet).ready(function() { 
    $("<iframe src='../active_timer.html' id='iframeDialog' />").dialog({ 
     autoOpen: false, 
     title: "Your session is about to expire!", 
     modal: true, 
     width: 400, 
     height: 200, 
     closeOnEscape: false, 
     draggable: false, 
     resizable: false, 
     buttons: { 
      "Yes, Keep Working": function(){ 
       $(this).dialog("close"); 
      }, 
      "No, Logoff": function(){ 
       //log user out 
      } 
     } 
    }); 

    // cache a reference to the countdown element. 
    // these below don't cache the reference I have tried them all. 
    var $countdown = $("#iframeDialog").contents().find("#dialog-countdown"); 
    var $countdown = $("#dialog-countdown", window.child); 
    var $countdown = $("#dialog-countdown", $('iframe').get(0).contentDocument); 

    //this is where I update the #dialog-countdown every second after the reference is made. 
}); 

active_timer.html:

<div id="dialog" style="overflow:hidden;"> 
    <p> 
     <span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 25px 0;"></span> 
     You will be logged off in <span id="dialog-countdown"></span> seconds. 
    </p> 
    <p>Do you want to continue your session?</p> 
</div> 

回答

0

當它加載,active_timer.html應該內active_timer.html到其父傳遞的DOM元素的引用。

function onLoad() { 
    window.opener.childCountdown = document.getElementById("dialog-countdown"); 
} 

在父:

var $countdown = $(childCountdown); 
+0

可能你也許elaberate多一點? – 2010-09-03 18:25:11

+0

添加了示例代碼 – Jerome 2010-09-03 18:43:50