I am trying to select all elements with the class .select which are nested somewhere in the DOM-tree.
The only restriction is, they're not allowed to have any parents with the class .forbidden.

我试图用类.select选择所有元素,这些元素嵌套在DOM树中的某个地方。唯一的限制是,他们不允许任何父母上课.forbidden。

So it would not find any elements in following tree part:

所以它在后面的树部分找不到任何元素:

<div>
    <div class="forbidden">
        <div>
            <div>
                <div class="select">Some Content</div>
            </div>
        </div>
    </div>
</div>

But would find 1 element in here:

但是会在这里找到1个元素:

<div>
    <div>
        <div>
            <div>
                <div>
                    <div class="select">Some Content</div>
                </div>
            </div>
        </div>
    </div>
</div>

How can I achieve this selection by using the .not() function? Something like this: $('.select').not($(this).parents().hasClass('.forbidden'));

如何使用.not()函数实现此选择?像这样:$('。select')。not($(this).parents()。hasClass('。forbidden'));

3 个解决方案

#1


6

You'll need to check for parent elements, and closest() stops at the first element found with that class, and if the selector has length, ie. contains elements, there is an element somewhere up the DOM tree with that class.

您需要检查父元素,并使用该类找到的第一个元素处的nearest()停止,以及选择器是否具有长度,即。包含元素,在DOM树的某个地方有一个元素。

$('.select').filter(function(){
     return !$(this).closest('.forbidden').length;
});

更多相关文章

  1. 是否可以将HTML元素固定到另一个的底部?
  2. 如何根据容器更改元素的宽度?
  3. 通过属性名称获取HTML元素
  4. Jquery在两个元素之间更改文本
  5. HTML哪些是块级元素,哪些是行内元素、
  6. 如何使用CSS消除元素的偏移?
  7. KeyPress或KeyDown事件没有得到html元素的buind
  8. 行内元素与块级元素
  9. 固定定位,父元素具有转换

随机推荐

  1. android 设置textview 边框
  2. Unity Android(三) Android7.0文件读写以及
  3. Android基础知识大纲
  4. 2011.06.22——— android framelayout布
  5. android通用适配器
  6. 如何使用android中的搜索功能
  7. Android webview 调用android浏览器
  8. Android多媒体学习六:利用Service实现背景
  9. ArrayAdapter源码
  10. Android 五大布局之(一) 线性布局和相对布