如何在jQuery中定位一个不是具有特定类的容器的子容器

How to target a container in jQuery that isn't a child of container with a specific class?

本文关键字:一个 jQuery 定位      更新时间:2023-09-26

假设我有这样的html:

<div>
    <div class="bar">
    </div>
</div>
<div class="foo">
    <div class="bar">
    </div>

我需要用类栏瞄准容器,这不是类foo的容器的孩子,而单独留下一个是foo的孩子。因为我所瞄准的实例是由第三方脚本创建的,所以我不能直接将类添加到它的包装器中。

无论如何要用JavaScript/jQuery做到这一点吗?

您可以使用:不()/自身之外()

$('.bar:not(.foo .bar)')
$('.bar').not('.foo .bar')

演示:小提琴