admin管理员组文章数量:1287882
I have nested unordered lists and click handlers on each li element. When clicking on a nested li, it's firing both events. Is there anyway to prevent this behavior?
<ul>
<li>thundercats
<ul>
<li>panthero</li>
<li>cheetarah</li>
</ul>
</li>
</ul>
So in the above list clicking "thundercats" would fire one event setting a value to "thundercats", clicking on panthero would fire another event setting a value to panthero.
Unfortunately clicking panthero causes the value to be set properly for an instant and then the click handler on thundercats fires and overwrites the value. Is there a way to prevent that?
I have nested unordered lists and click handlers on each li element. When clicking on a nested li, it's firing both events. Is there anyway to prevent this behavior?
<ul>
<li>thundercats
<ul>
<li>panthero</li>
<li>cheetarah</li>
</ul>
</li>
</ul>
So in the above list clicking "thundercats" would fire one event setting a value to "thundercats", clicking on panthero would fire another event setting a value to panthero.
Unfortunately clicking panthero causes the value to be set properly for an instant and then the click handler on thundercats fires and overwrites the value. Is there a way to prevent that?
Share Improve this question edited Sep 29, 2011 at 19:27 lonesomeday 238k53 gold badges327 silver badges328 bronze badges asked Sep 29, 2011 at 19:21 tad604tad604 3361 gold badge5 silver badges18 bronze badges 1- Can you post your code? It sounds like you need to prevent propagation up the DOM. – Ted Kalaw Commented Sep 29, 2011 at 19:24
3 Answers
Reset to default 10One option is to prevent the event bubbling up the DOM tree. This will work, but it's not optimal because sometimes event bubbling is very useful. It's nicer to check to see if the event originated on the correct element:
$('li').click(function(event) {
if (this === event.target) {
// do your code
}
});
This code works because this
and event.target
both point to DOM elements. this
is the element where the event handler is handled (where the code is run) while event.target
is the element where the event originated, i.e. the element that was clicked. If they are the same element, the event was triggered on the same element as where it is being handled; this is where you want the code to be run, and nowhere else. Comparing the elements with ===
acplishes this.
You need to use event.stopPropagation
. This will keep the click event from bubbling up the DOM tree.
$(document).ready( function () {
$('li').click(function (evt) {
// Your code here
// Stop the event propagation
evt.stopPropagation();
})
});
It should be as simple as adding:
return false;
...to your event handler function.
When an event handler returns false, it stops event propagation (it's effectively the same as calling .preventDefault() and .stopPropagation() on the event object). It'd definitely be helpful to see your code, but it sounds like this will do it.
本文标签: javascriptclick events on nested list items each with their own click handlerStack Overflow
版权声明:本文标题:javascript - click events on nested list items each with their own click handler - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741244615a2364634.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论