Domanda

<div id="main">
 <div class="a"></div>
 <div class="b"><p>not me</p></div>
 <div class="b"></div>
 <div class="b"></div>
 <div class="c"></div>
</div> 

How we can write a selector to select all divs with class b except whose child is <p>not me</p> ?

È stato utile?

Soluzione

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

Or the readable version

$('div.b').filter(function(){
    return !$(this).find('p').length;
});

If you want to match the content as well:

$('div.b').filter(function(){
    return $(this).find('p').text() !== "not me";
});

Live DEMO

Altri suggerimenti

demo http://jsfiddle.net/46nC5/1/

Since you are specifically looking for class b made a demo for you hence sharing.

In this demo you will see the not me getting fade out, rest will stay as it is.

i.e. With :not + :has it can be done

code

$('div.b:has(p)').​

or

$('div.b:not(:has(p))')
$('div.b').filter(function () {
    return $(this).find('p:contains(not me)').length == 0;
})

jsFiddle Demo

The best way I usualy do:

$('div.b').filter(function () {
   return !$(this).hasClass('notMe');
});


<div id="main">
     <div class="a"></div>
     <div class="b notMe"><p>not me</p></div>
     <div class="b"></div>
     <div class="b"></div>
     <div class="c"></div>
</div> 

This will give you two div who have class="b" and does not have p (paragraph)

Live Demo

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

And here's another one

$("div.b:contains('not me')")

edit: sorry, here's with not

$("div.b:not(:contains('not me'))")
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top