简体   繁体   English

单击链接时如何使用 jQuery 发出警报?

[英]How to alert with jQuery when a link is clicked?

I want to alert something if a <a> is clicked, but only if there is a real link.我想在点击<a>提醒一些事情,但<a>是有一个真正的链接。 I have this code but it doesn't really work...我有这个代码,但它并没有真正起作用...

$("a").click(function(){
    if ($(this).attr("href") != "#" && $(this).attr("href") != "") {
        alert("foo");
    }
});

What is wrong?怎么了?

Thanks谢谢

Make sure you're calling e.preventDefault() in the method.确保您在方法中调用e.preventDefault()

$("a").click(function(e){
    if ($(this).attr("href") != "#" && $(this).attr("href") != "") {
        alert("foo");
        e.preventDefault();
    }
});

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

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