简体   繁体   English

使用jQuery选择除标记组中的子项以外的所有子项

[英]Select all children except those in a marked group with jQuery

I have a list structure like this (note the two lists with .browser class and one of them with data-click="yes") 我有一个像这样的列表结构(注意两个列表与.browser类,其中一个与data-click =“是”)

<ul class="browser" data-click="yes">
<li><a href="#">Group 1</a>
    <ul>
        <li><a href="#">Item_1</a></li>
        <li><a href="#">Item_2</a></li>
        <li><a href="#">Item_3</a></li>
    </ul>
</li>
<li><a href="#">Group_2</a>
    <ul>
        <li><a href="#">Item_4</a></li>
        <li><a href="#">Item_5</a></li>
        <li><a href="#">Item_6</a></li>
        <li><a href="#">Group_3</a>
            <ul class="browser">
                <li><a href="#">Item_1</a></li>
                <li><a href="#">Item_2</a></li>
                <li><a href="#">Group_4</a>
                    <ul>
                        <li><a href="#">Item_1</a></li>
                        <li><a href="#">Item_2</a></li>
                        <li><a href="#">Item_3</a></li>
                    </ul>
                </li>
                <li><a href="#">Item_3</a></li>
            </ul>
        </li>
    </ul>
</li>
</ul>

I would like to assign an on click action with jQuery to the A elements within the <ul class="browser" data-click="yes"> element, however I do not want this behavior to propagate further to the A elements in the child element <ul class="browser"> . 我想将jQuery的on click动作分配给<ul class="browser" data-click="yes">元素中的A元素,但是我不希望这种行为进一步传播到A元素中子元素<ul class="browser"> Moreover, it is important for me to keep the class .browser for both those lists. 此外,对于我来说,保持这两个列表的类.browser非常重要。

I tried to use :not selector in the following way 我尝试使用:不是以下列方式选择器

$("body").on("click", "UL.browser[data-click='yes'] :not(.browser) A", function (event) {
// do something
});

and many other variants, but without success. 和许多其他变种,但没有成功。 It is also important for me to use the structure with appending the event to body since I am changing the structure dynamically. 因为我正在动态地改变结构,所以使用结构将事件附加到body也很重要。

I found many variants of this problem here, however none of them is quite the same and they did not work for me. 我在这里发现了这个问题的很多变种,但是它们都没有完全相同,它们对我不起作用。 I sense that I misunderstand the :not selector in some way, however I still can't get it right. 我觉得我误解了:不是以某种方式选择器,但是我仍然无法做到正确。

I understand you only want to attach the click event to the "first" level of <a> which are children of a "browser" with data-click set to "yes", you can easily do that by only selecting direct childs: 我知道你只想将click事件附加到<a>的“第一”级别,这是“浏览器”的子级,数据点击设置为“是”,你可以通过选择直接子项轻松实现:

$("body").on("click", "ul.browser[data-click='yes'] > li > a", function (event) {
    // Do something
});

If I understand well, you want to handle the click on a elements inside ul.browser[data-click='yes'] but not inside an internal .browser element : 如果我没有理解好了,你要处理的点击a内部元素ul.browser[data-click='yes']但不是内部内.browser元素:

$("body").on("click", "ul.browser[data-click='yes'] a:not(.browser .browser *)", function (event) {
  // do something
});

Demonstration 示范

This is not the optimal way but should work: 这不是最佳方式,但应该有效:

$('ul.browser').each(function()
{
   if($(this).data('click') === 'yes')
   {
       $(this).on('click', function(e)
       {
            //Stuff
       });   
   } 
});

As I understood, you want to enable click event function for all <a> tags inside <ul> having class .browser with attribute data-click="yes" and you want to exclude all the other <a> tags that comes under internal <ul> tag having class .browser . 据我所知,你想为<ul>中的所有<a>标签启用点击事件功能,其中包含带有属性data-click="yes" .browser类,你想要排除内部的所有其他<a>标签<ul>标签有类.browser

$(document).ready(function(){

  $("body").on("click", "ul.browser[data-click='yes']  li a:not(.browser .browser *)",function(){
    alert("hi");
  });

});

The above code explains => add click function for all <a> tags that comes inside <li> tags whose parent is <ul> tag having class .browser and attribute data-click='yes' and don't add click event for any of the tags having hierarchy .browser .browser 上面的代码解释了=>为<li>标签内的所有<a>标签添加点击功能,其标签的父级是<ul>标签,其中包含类.browser和属性data-click='yes'并且不添加点击事件任何具有层次结构.browser .browser的标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM