javascript - jQuery select items which parents are not of a given class - Stack Overflow

how can select all nodes with class "myClass" whose parent nodes don't have class "

how can select all nodes with class "myClass" whose parent nodes don't have class "myClass".

for example, i have following 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"

actually, i'm trying to write a code that converts HTML from that example to a list with tabulations, such as:

d1
  d2
  d4
d3

(there can be more descedant nodes)

how can select all nodes with class "myClass" whose parent nodes don't have class "myClass".

for example, i have following 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"

actually, i'm trying to write a code that converts HTML from that example to a list with tabulations, such as:

d1
  d2
  d4
d3

(there can be more descedant nodes)

Share Improve this question asked Oct 27, 2009 at 13:37 kipelovetskipelovets 1,2142 gold badges13 silver badges16 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 7

I have e 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")

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745090421a4610660.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信