gpt4 book ai didi

jquery - 为什么 jQuery 有时会覆盖 window.onbeforeunload?

转载 作者:行者123 更新时间:2023-12-03 22:41:20 26 4
gpt4 key购买 nike

我在使用 jQuery (1.6.x) 时遇到了奇怪的问题。我的页面上有这个简单的调用:

window.onbeforeunload = function() { return 'Are you sure ?'; };

但它不起作用,因为正如我发现的,jQuery 覆盖了 window.onbeforeunload 的内容。在 JS 控制台中,我可以看到 window.onbeforeunload 包含一段 jQuery 代码:

function( e ) {
// Discard the second event of a jQuery.event.trigger() and
// when an event is called after a page has unloaded
return typeof jQuery !== "undefined" && (!e || jQuery.event.triggered !== e.type) ?
jQuery.event.handle.apply( eventHandle.elem, arguments ) : undefined;
};

有没有办法找到 jQuery 覆盖我的 onbeforeunload 函数的原因和位置?当我尝试仅加载 jQuery 并使用 onbeforeunload 函数运行空 jsfiddle 时,它​​按预期工作。

任何提示将不胜感激。

编辑:

以防万一,有人建议使用:

$(window).bind('beforeunload', function() { return 'Are you sure';} );

我已经尝试过了,它的行为方式与使用纯 Javascript 相同。

最佳答案

好吧,我终于找到了一个解决方案,这可能不是最干净的解决方案 - 因为我不知道问题的确切原因 - 但它有效。我已将 beforeunload 函数放入 jQuery 的 load 函数中,以便在加载 DOM 和其他元素后执行它。

$(window).load(function () {
$(window).bind('beforeunload', function() { return 'Are you sure ?';} );
});

关于jquery - 为什么 jQuery 有时会覆盖 window.onbeforeunload?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7481048/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com