简体   繁体   English

jQuery防止锚点击

[英]jQuery prevent anchor click

I'm trying to prevent the default anchor link and onclick event to trigger. 我正在尝试阻止默认锚链接和onclick事件触发。

Here's the HTML code: 这是HTML代码:

<a id="mylink" href="http://google.com" 
    onclick="window.location.href='http://google.com.au'; return false;">
Google</a>

I'm trying to prevent any redirects from occurring, using the following jQuery code: 我正在尝试使用以下jQuery代码阻止任何重定向发生:

$("#mylink").click(function(ev) {
    ev.preventDefault();
    ev.stopPropagation();
}

But it doesn't seem to work. 但它似乎没有用。 The code still invokes the anchor onclick event and redirects to http://google.com.au 代码仍会调用锚点onclick事件并重定向到http://google.com.au

Any clues or suggestions? 有什么线索或建议吗? Thanks in advance. 提前致谢。

只要 .click()回调之前调用它,您需要手动清除onclick

$("#mylink").prop('onclick', null);

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

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