简体   繁体   English

jQuery选择父母不是给定类别的项目

[英]jQuery select items which parents are not of a given class

how can select all nodes with class "myClass" whose parent nodes don't have class "myClass". 如何选择父节点没有类“ myClass”的所有类“ myClass”的节点。

for example, i have following HTML: 例如,我有以下HTML:

<div class="myContainer">
    <div class="myClass" id="d1"> 
        <div class="myClass" id="d2"></div>
        <div class="myClass" id="d4"></div>
    </div>
    <div class="myClass" id="d3"></div>
</div>

and i want to get a list of elements with ids "d1" and "d3" 而且我想获得ID为“ d1”和“ d3”的元素列表

actually, i'm trying to write a code that converts HTML from that example to a list with tabulations, such as: 实际上,我正在尝试编写代码,将示例中的HTML转换为带有列表的列表,例如:

d1
  d2
  d4
d3

(there can be more descedant nodes) (可以有更多的后继节点)

I have come up with a number of possible solutions. 我提出了许多可能的解决方案。 Maybe: 也许:

$(":not(.myClass) > .myClass")

Or if you want to look beyond the direct parent: 或者,如果您想超越直接父项:

$(":not(.myClass) .myClass")

This might work also: 这也可能起作用:

$(".myClass:has(:not(.myClass))")

A better solution for your exact example: 为您的确切示例提供更好的解决方案:

$(".myContainer > .myClass")

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

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