使用jquery消防ul标签点击事件不是li [英] Fire ul tag click event not li with jquery

查看:94
本文介绍了使用jquery消防ul标签点击事件不是li的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何才能影响点击事件只有ul标签不是所有li与jQuery?

How can affect click event only ul tag not all li with jQuery?

<!-- HTML -->
<ul class="wrap">
    <li>test1</li>
    <li>test2</li>
    <li>test3</li>
</ul>

我试过jquery,但它不起作用。

I tried jquery like this.But it doesnt work.

//Javascript
jQuery("ul.wrap").not(jQuery("ul > li")).click(function(){
      //fire only ul
});

我们该如何做?

How can we do this?

推荐答案

事件将'DOM'向上冒泡到其父元素。你需要做的是将事件附加到 ul 以及子< li 元素上的另一个事件,该元素使用 stopPropagation()

Events 'bubble' up the DOM to their parent elements. What you need to do is attach the event to the ul, and another event on the child li elements which uses stopPropagation():

jQuery("ul.wrap")
    .on('click', function(){
        // do something...
    })
    .on('click', 'li', function(e) {
        e.stopPropagation();
    });

这篇关于使用jquery消防ul标签点击事件不是li的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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