我可以在单击锚标记时阻止调用onbeforeunload吗? [英] Can I prevent onbeforeunload from being called when clicking on an anchor tag?

查看:128
本文介绍了我可以在单击锚标记时阻止调用onbeforeunload吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在头部:

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

在正文中:

<a id="anchor_id" href="javascript: void(0);">Click Me</a>

是否有可能阻止 onbeforeunload 在Internet Explorer中单击此链接时调用
?在其余的
浏览器中,代码工作正常。

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.

推荐答案

尝试设置 href =#而是在 clickHandler 函数中返回false(或者通过 event.preventDefault阻止默认事件) )

Try setting href="#" instead, and return false in the clickHandler function (or prevent the default event by event.preventDefault())

这篇关于我可以在单击锚标记时阻止调用onbeforeunload吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆