从 window.onbeforeunload 中排除 DIV 内的点击
给定以下函数:
window.onbeforeunload= function() {
if (CKEDITOR.instances.stuff.getData().length > 0 && oktoquit == false) {
return "You have unsaved changes. Click Cancel now, then 'Save' to save them. Click OK now to discard them.";
}
};
如果用户单击带有 ID 的 div 中的链接,我想要一种方法来排除此函数的运行:
<div id="ignore me"><a href="">blah</a><a href="">blah</a><a href="">blah</a></div>
有什么想法吗?
** 更新 事实证明,以下网站搜索代码导致了该问题。但为什么?
$("#searchresults li").live('click', function(e) {
if (e.target.nodeName != "a") {
window.location = $(this).find('a').attr('href');
}
});
Given the following function:
window.onbeforeunload= function() {
if (CKEDITOR.instances.stuff.getData().length > 0 && oktoquit == false) {
return "You have unsaved changes. Click Cancel now, then 'Save' to save them. Click OK now to discard them.";
}
};
I'd like a way to exclude this function from running if the user clicks a link in a div with an ID:
<div id="ignore me"><a href="">blah</a><a href="">blah</a><a href="">blah</a></div>
Any ideas?
** Update
Turns out the following code for the site's search is causing the issue. but why?
$("#searchresults li").live('click', function(e) {
if (e.target.nodeName != "a") {
window.location = $(this).find('a').attr('href');
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以这样做:
我将您的 ID 更改为
ignoreMe
,因此它是有效的,但您明白了:) 根据您的问题判断,您可能需要#ignoreMe a
选择器,如果您想从执行处理程序中排除该 div 内的任何链接,无论您想要什么,只需使用该选择器即可。You can do this:
I changed your ID to be
ignoreMe
so it's valid, but you get the point :) Judging by your question, you may want#ignoreMe a
for a selector, if you want to exclude any links inside that div from executing the handler, whatever you want, just use that selector.window.top.onbeforeunload 这会忽略执行此操作的 CKEDITOR iFrame
window.top.onbeforeunload this ignores the CKEDITOR iFrame which does the job