jQuery侦听器(不包括子级)

编程入门 行业动态 更新时间:2024-10-28 09:23:01
本文介绍了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"); });

jsfiddle/PsaHQ/4/

更多推荐

jQuery侦听器(不包括子级)

本文发布于:2023-10-26 12:46:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530184.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:侦听器   不包括   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!