简体   繁体   English

选择所有div,但不选择嵌套在特定div中的div

[英]Select all divs but not those in nested in specific div

I'm using this IE7 z-index fixer and it is messing with some specific elements. 我正在使用这个IE7 z-index修复程序,它正在搞乱一些特定的元素。

What i'm trying to do is selecting all divs, but not the ones nested in a specific div. 我要做的是选择所有div,但不是嵌套在特定div中的div。

Here is the jQuery code I'm currently using, that is not working as intended. 这是我目前正在使用的jQuery代码,它没有按预期工作。

<script type="text/javascript">
    $(function() {
        var zIndexNumber = 1000;
        $('div').not('.filter_list_1_container > div').each(function() {
            $(this).css('zIndex', zIndexNumber);
            zIndexNumber -= 10;
        });
    });
</script>

For some reason it only ignores the first layer. 由于某种原因,它只忽略第一层。 If the nested divs contains a div the jQuery code does not ignore them either... 如果嵌套的div包含div,jQuery代码也不会忽略它们......

How do I ignore all the divs inside the ".filter_list_1_container" class? 如何忽略“.filter_list_1_container”类中的所有div?

You probably need descendant insted of direct child so remove > from selector. 你可能需要直接孩子的后代,所以从选择器中删除>

Change 更改

.not('.filter_list_1_container > div')

To .not('.filter_list_1_container div') To .not('。filter_list_1_container div')

Your code would be. 你的代码就是。

$(function() {
    var zIndexNumber = 1000;
    $('div').not('.filter_list_1_container div').each(function() {
        $(this).css('zIndex', zIndexNumber);
        zIndexNumber -= 10;
    });
});

You're using > in your selector, which means that it will only select direct children of that element. 你在选择器中使用了> ,这意味着它只会选择该元素的直接子元素。 If you remove it then it will ignore any descendents of .filter_list_1_container that are div elements 如果你删除它,它将忽略作为div元素的.filter_list_1_container任何后代

This will do it: 这样做:

<script type="text/javascript">
    $(function() {
        var zIndexNumber = 1000;
        $('div').not('.filter_list_1_container div').each(function() {
            $(this).css('zIndex', zIndexNumber);
            zIndexNumber -= 10;
        });
    });
</script>

Well your problem is that the '>' operator is selecting all direct children. 那你的问题是'>'运算符正在选择所有直接的孩子。 So in the following case.. 所以在下面的例子..

.filter_list_1_container | .filter_list_1_container | div | div | div DIV

It will only exclude the first set of divs and show the second one. 它只会排除第一组div并显示第二组div。

Try removing the '>' operator 尝试删除“>”运算符

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

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