使用 jquery 缓存对 iframe 中元素的引用

发布于 2024-09-17 10:33:49 字数 1531 浏览 9 评论 0原文

我正在使用一个 jquery 对话框,它使用 iframe 让用户知道会话即将过期。我想每秒更新这个对话框并进行倒计时,因此我想缓存对倒计时元素的引用,这样我们就不必每秒查询 DOM。

我尝试过很多不同的方法,但似乎无法使其发挥作用。

$(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>

I am using a jquery dialog that uses an iframe to let the user know there session is about to expire. I would like to update this dialog every second with a countdown, thus I would like to cache a reference to the countdown element so we don't have to query the DOM for it each second.

I have tried to do this many different ways and I can't seem to get it working.

$(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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瓜 2024-09-24 10:33:49

加载时,active_timer.html 应将 active_timer.html 中 DOM 元素的引用传递给其父级。

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

在父级中:

var $countdown = $(childCountdown);

When it loads, active_timer.html should pass a reference of the DOM element within active_timer.html to its parent.

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

In the parent:

var $countdown = $(childCountdown);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文