Opposite jQuery find () which is not ()?

Given the following HTML, which jQuery selector will select the last blah blah span , but not the first?

 <div class='d1'> <span> <a>blee <span> <!-- don't select this span --> <span>blah</span> blah </span> </a> </span> </div> <br/> <div class='d1'> <span> <a>blee <span> <!-- select this span --> blah blah </span> </a> </span> </div> 

I tried the options using not () but can't figure it out. It seems that I need a selector that finds the first span, but then excludes it if it contains a different range. Sort of:

 $('.d1 span a span').not(':has("span")') 

but it does not. What am I missing?

+4
source share
5 answers

If I understand your question correctly, all you are missing is a combinator for children:

 $('.d1 span a > span').not(':has("span")') 

Here is a working example .

The reason for this is that the .d1 span a span will match the span children of the a element in the first .d1 element. The .not() call excludes the external, but the internal will still be part of the consistent set. By adding a child combinator, only the external span is selected and then removed by calling .not() .

+5
source

If I understand your question correctly, you need this selector:

$('div.d1 span:last')

Script showing this in action: http://jsfiddle.net/aWCsT/

+2
source

Select only the child span this a :

 $('.d1 span a > span:not(:has(span))') 

Demo: http://jsfiddle.net/Blender/eSDrK/1/

+1
source

Try the following:

 $('.dl:last span:not(:has(*))') 
+1
source

demo jsbin

 $('a>span:not(:first)') 
0
source

Source: https://habr.com/ru/post/1436245/


All Articles