如何让jQuery忽略父母.click() [英] How can I get jQuery to ignore parents on .click()

查看:128
本文介绍了如何让jQuery忽略父母.click()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例代码:

<div id="a">
     <div id="b">
          Click here
     </div>
</div>

<script>
    $('*').click(function() {
        alert($(this).attr('id'));                    
    });
</script>

当您点击点击此处时,它会提醒两次,一次是' b '然后使用' a '。

When you click 'Click Here' it alerts twice, once with 'b' and then with 'a'.

我需要弄清楚如何让jQuery变为忽略用户点击的所有父母,只提醒,在这种情况下,b。

I need to figure out how to get jQuery to ignore all the parents of where the user clicked and just alert, in this case, 'b'.

推荐答案

尝试使用 $('body')。委托('*','click',fn)而不是直接事件处理程序。 (参见 jQuery.delegate 。)每次活动都会调用一次,你可以找到从事件对象中取出哪个元素受到影响。

Try using $('body').delegate('*', 'click', fn) instead of direct event handlers. (See jQuery.delegate.) It will be called exactly once for each event, and you can find out from the event object which element was affected.

这篇关于如何让jQuery忽略父母.click()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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