锚点上的jquery点击事件 [英] jquery click event on anchor

查看:137
本文介绍了锚点上的jquery点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是html的片段:

 < div id =tag-cloud-widget> 
< div class =content>
< a href =# =1class =cloud-elementdata-tag-id =10style =font-size:12px; color:rgb(205,236, 222);> T1< / a>
< a href =# =1class =cloud-elementdata-tag-id =1style =font-size:12px; color:rgb(205,236, 222);> T2< / a>
< a href =# =1class =cloud-elementdata-tag-id =3style =font-size:12px; color:rgb(205,236, 222);> T3< / a>
< / div>
< / div>

我想设置一个点击处理程序来响应用户对锚标签的点击。以下是测试代码:

  $(#tag-cloud-widget .content a)click(function(e) {
alert('clicked');
return false;
});

上面的点击处理程序不会被解雇,也没有这样做:



$ $ $ $ $ $ $$$$$$$$$$ );
return false;
});

但是,

 code> $(#tag-cloud-widget .content)。点击(function(e){...}); 

  $(#tag-cloud-widget)。点击(function(e){...}); 

确实被解雇了!



我没看到什么?

解决方案

处理锚定点击事件,始终使用 e.preventDefault(); ,当您不需要锚点。
像魅力一样的火焰


Here's the snippet of html i have:

<div id="tag-cloud-widget">  
    <div class="content">  
        <a href="#" rel="1" class="cloud-element" data-tag-id="10" style="font-size: 12px; color: rgb(205, 236, 222); ">T1</a>  
        <a href="#" rel="1" class="cloud-element" data-tag-id="1" style="font-size: 12px; color: rgb(205, 236, 222); ">T2</a>  
        <a href="#" rel="1" class="cloud-element" data-tag-id="3" style="font-size: 12px; color: rgb(205, 236, 222); ">T3</a>  
    </div>
</div>

I'd like to set up a click handler to respond to the user's click on the anchor tags. Here's the test code:

$("#tag-cloud-widget .content a").click(function(e) {
    alert('clicked');  
    return false;  
});  

The click handler above does not get fired and neither does this:

$("#tag-cloud-widget .content .cloud-element").click(function(e) {  
    alert('clicked');  
    return false;  
});  

However,

$("#tag-cloud-widget .content").click(function(e) { ... });  

and

$("#tag-cloud-widget").click(function(e) { ... });  

do get fired!

What am I not seeing???

解决方案

When handling anchor click events, always use e.preventDefault(); when you don't need the anchor anyway. Fires like a charm

这篇关于锚点上的jquery点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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