CSS:如何排除 DIV 及其后代?

标签 css jquery-selectors

我的 html 看起来像这样:

<div id="mainDiv">
    <div id="childDiv1">
        <img />
        <div>
            <img />
            <div />
        </div>
        ...
    </div>
    <div id="childDiv2">
        ...
    </div>
    ...
</div>

我想选择除 childDiv1 及其后代之外的 mainDiv 中的所有内容。我认为 #mainDiv :not(#childDiv1) 可能有效,但它只排除 chidlDiv1 而它的所有后代都被选中。 #childDiv1 * 将选择所有后代(但不是 childDiv1 本身)但 :not(#childDiv1 *) 无效。 最好使用 jQuery 解决方案。谢谢

最佳答案

认为这行得通:

$("#mainDiv > :not(#childDiv1)")

关于CSS:如何排除 DIV 及其后代?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5753168/

相关文章:

JQuery如何查找并删除li元素

jQuery :nth-child() selector

html - 如何移动底部的2个按钮?

html - Bootstrap 行全宽

html - 按钮宽度占父项的百分比

jquery - 使用 jQuery 获取每个父级的最后一个子级

css - div 背景的不透明度不影响 IE 8 中包含的元素?

css - Bootstrap 中的间距内容

Jquery 函数未在 'tap' 函数事件内部执行

jquery - css选择器理解