我可以防止单击锚标记时调用 onbeforeunload 吗?
head中:
<head>
<script type="text/javascript">
$(document).ready(function() {
$("#anchor_id").click(clickHandler);
window.onbeforeunload = confirmExit;
});
function clickHandler() {
/* hide/show some hidden div's */
$("body").append("<p>Hi there</p>");
}
function confirmExit() {
return "There are some unsaved changes on page.";
}
</script>
</head>
body中:
<a id="anchor_id" href="javascript: void(0);">Click Me</a>
是否可以防止onbeforeunload
被调用 在 Internet Explorer 中单击此链接时?在其余的 浏览器中的代码运行良好。
In head:
<head>
<script type="text/javascript">
$(document).ready(function() {
$("#anchor_id").click(clickHandler);
window.onbeforeunload = confirmExit;
});
function clickHandler() {
/* hide/show some hidden div's */
$("body").append("<p>Hi there</p>");
}
function confirmExit() {
return "There are some unsaved changes on page.";
}
</script>
</head>
In body:
<a id="anchor_id" href="javascript: void(0);">Click Me</a>
Is it possible to prevent onbeforeunload
from being called
when clicking on this link in Internet Explorer? In the rest
of the browsers the code is working fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试设置
href="#"
,并在clickHandler
函数中返回 false(或通过event.preventDefault()
阻止默认事件)Try setting
href="#"
instead, and return false in theclickHandler
function (or prevent the default event byevent.preventDefault()
)您可以使用 jQuery 的浏览器帮助程序将事件分配包装在条件浏览器逻辑中:
http://docs.jquery.com/Utilities/jQuery.browser.version
You could wrap the event assignment in condition browser logic using jQuery's browser helper:
http://docs.jquery.com/Utilities/jQuery.browser.version
试试这个,在FF3.5和IE8下测试过
Try this, tested in FF3.5 and IE8