使用 jquery 缓存对 iframe 中元素的引用
我正在使用一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
加载时,active_timer.html 应将 active_timer.html 中 DOM 元素的引用传递给其父级。
在父级中:
When it loads, active_timer.html should pass a reference of the DOM element within active_timer.html to its parent.
In the parent: