简体   繁体   中英

Select elements which don't have parent class

 $("#result").html($('[contenteditable]').text()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="scorer"> <div> <div contenteditable>1</div> <div contenteditable>2</div> </div> </div> <div> <div contenteditable>3</div> <div contenteditable>4</div> </div> <hr /> <div id="result"> result </div> 

How can I select contenteditable elements which do not have a parent up the chain of "scorer" in CSS or jquery.

Result should be 34.

Thanks!

You can use not() function like this .not('.scorer div')

 $("#result").html($('[contenteditable]').not('.scorer div').text()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="scorer"> <div> <div contenteditable>1</div> <div contenteditable>2</div> </div> </div> <div> <div contenteditable>3</div> <div contenteditable>4</div> </div> <hr /> <div id="result"> result </div> 

You can use filter function for this

var result = $("div[contenteditable]").filter(function () {
    return $(this).closest(".scorer").length == 0
}).text();

Fiddle

Try not()

$("#result").html($('[contenteditable]').parent().not('.scorer div').text());

FIDDLE

尝试获取除得分手的孩子以外的所有人:

$('div[contenteditable]').not('.scorer div');

Just to give you a different approach which is the one i find more easier to read

$('.result').html($('div:not(.scorer) [contenteditable]').text());

jsfiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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