jQuery侦听器(不包括子级) [英] jQuery Listener Excluding Child

查看:75
本文介绍了jQuery侦听器(不包括子级)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个dd项的dl列表。每个dd项都具有一个侦听器(请参见下文),因此如果单击它,则可以重建页面并更改某些内容。这些dd项中的每个项中也都有一个复选框,不过我希望将其从该侦听器中排除(以便可以由其他侦听器接收)。

I have a dl list of dd items. Each dd item has a listener attached to it (see below) so if it is clicked on I can rebuild the page and change some stuff. Each of these dd items also have a checkbox within them though which I would like to be excluded from that listener (so it can be picked up by another listener).

发生的问题是,只要我单击dd中的任意位置,即使单击复选框,它也会应用dd侦听器,而不是复选框侦听器。有没有办法在没有在dd内设置div并单独应用监听器的情况下,准确地点击了什么?

The problem that is occuring is whenever I click anywhere in the dd it will apply the dd listener, not the checkbox listener even if I clicked on the checkbox. Is there a way to distinguish what exactly was clicked without setting up divs inside the dd and applying listeners individually?

示例HTML代码:

<dl>
  <dd class="class1 class2 class3">Some text and stuff 
    <input type="checkbox" class="class1 checkBox">
  </dd>
</dl>

示例jQuery代码:

Example jQuery Code:

$("class1.checkbox").live("click", function() {
  //Do some other, completely different, cool stuff
  //console.log($(this).parent().attr("id"));
  console.log("test");
});

$("dd.class1.class2").live("click", function () {
  //Do some cool stuff
});


推荐答案

$(":checkbox").live("click", function(e) {
        e.stopPropagation();
  //Do some other, completely different, cool stuff
  //console.log($(this).parent().attr("id"));
  alert("test");
});

$("dd.class1.class2").live("click", function (e) {
    e.stopPropagation();
  //Do some cool stuff
    alert("test2");
});

http://jsfiddle.net/PsaHQ/4/

这篇关于jQuery侦听器(不包括子级)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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