[英]How do I call a jQuery function on an a with href?
I have a binded jquery function on an a element with a href attribute 我在一个带有href属性的元素上有一个绑定的jquery函数
If the a has no href attribute, the function bound to the click event will fire 如果a没有href属性,则将触发绑定到click事件的函数
If it has a href attribute it wont 如果它有一个href属性,它不会
Help? 救命?
Try this: 尝试这个:
$('a').click(function(e) {
e.preventDefault(); //preventing the link from being followed.
// your code
});
I imagine you want to return false
from your handler, which will cancel the ANCHOR's following the HREF. 我想你想从你的处理程序中
return false
,这将取消HREF之后的ANCHOR。
<a href="http://www.yahoo.com" id="test">Test</a>
$('#test').click(function(){
alert('test');
return false;
});
I think your problem is that the event is indeed firing, but since you have an <a>
with an href
the browser is also reloading or switching pages. 我认为你的问题是事件确实在触发,但由于你有一个带有
href
的<a>
,浏览器也在重新加载或切换页面。 If you want to prevent that behavior you need to return false;
如果要防止该行为,则需要
return false;
from you click event. 从你点击事件。
$("a").click(function() {
if(!$(this).attr("href")) {
//fire
}
});
Better use javascript:void(0); 更好地使用javascript:void(0); in anchor tag -> http://www.tizag.com/javascriptT/javascriptvoid.php
在锚标签 - > http://www.tizag.com/javascriptT/javascriptvoid.php
这是一个简单的解决方案
href="javascript:alert('Hello');"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.