简体   繁体   English

jQuery拦截页面的锚点更改

[英]Jquery intercepting anchor click change of page

I am wanting to intercept anchor clicks and show a prompt if text has changed and not been submitted in a form using zebra-dialog. 我想拦截锚点单击,并在文本已更改且未使用zebra-dialog提交表单时显示提示。 For any other form of page leaving I use the built in browser unload dialog. 对于其他任何形式的页面离开,我都使​​用内置的浏览器卸载对话框。

Here is the code I have so far, the dialog shows, but no matter which option you click you end up staying on the page. 对话框显示出来,这是我到目前为止的代码,但是无论单击哪个选项,最终都停留在页面上。

$("body").on("mousedown",'a', function(e) {

    if(inputhaschanged) {
        return $.Zebra_Dialog("You haven't submitted your comment. Do you wish to leave?", {'type':'question','title':'','buttons':  [
                {caption: 'No', callback: function() {  }},
                {caption: 'Yes', callback: function() { return true; }}]
        });
    }

});          

One solution for this is you disable default behavior of all link on your page and then check for modification. 一种解决方案是禁用页面上所有链接的默认行为,然后检查修改。 Also you need to call event window.onbeforeunload to prevent other navigation method. 另外,您需要调用event window.onbeforeunload以防止其他导航方法。 Reference: how to stop page redirect How do you prevent a webpage from navigating away in JavaScript? 参考: 如何停止页面重定向 如何防止网页在JavaScript中导航?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM