开发者

jQuery selector question

开发者 https://www.devze.com 2022-12-08 20:59 出处:网络
I\'m in a bit of a dilemma. I need to select div tags based on whether their children have a certain class. This is a code example of the DOM structure:

I'm in a bit of a dilemma. I need to select div tags based on whether their children have a certain class. This is a code example of the DOM structure:

<div id="container">

    <div id="one">
         <p>This is item one</p>
         <p class="special">This is a description</p>
    </div>

    <div id="two">
         <p>This is item one</p>
         <p>This is a description</p>
    </div>

    <div id="three">
         <p>This is item one</p>
         <p class="special">This is a description</p>
    </div>

</div>

So, what I want to select is a div tag that doesn't have a paragraph w开发者_运维技巧ith a class of "special", and in the example above, that would be second div tag (#two).

Any ideas?


You can use the :not and :has selectors:

$('div:not(:has(p.special))')


Try this:

$("div").filter(function() {
    return $(this).children("p.special").length == 0;
})
0

精彩评论

暂无评论...
验证码 换一张
取 消