jQuery:选择不是特定类后代的所有元素 [英] jQuery: Select all elements that are not descendant of a certain class
本文介绍了jQuery:选择不是特定类后代的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<div class="container" id = "0" >
<div class="x" id = "1">
<div id = "2">
<p id = "3">
<span id = "4" >text</span>
</p>
<div>
</div>
<div id="5">
<div id="6">
<p id="7">
<span class="x" id="8" >text</span>
<span id="9">text</span>
</p>
<div>
</div>
<div>
您能帮我选择所有要素吗?
Can you help me to select all the elements :
- .container"的后代
- 不是'.x'的后代
- 本身没有类".x".
查看上面的HTML;它应选择元素 5、6、7和9
Looking at the HTML above; it should select the elements 5,6,7 and 9
- 元素1的类为"X"
- 元素2是具有"X"类的元素(元素1)的直接子元素
- 元素3和4是子元素"X"(元素1)的后代
元素8的类为"X"
我有这个选择器,但它会继续选择类"X"的元素的后代(深子级)
I have this selector but it keeps selecting the descendants (deep children) of element with class "X"
var elements = $('.container').find().parents(':not(.X)').andSelf().filter(':not(.X)');
推荐答案
这应该做到:
$('.container').find(':not(.x):not(.x *)');
再次恢复为第一个修订版.我以为这种方法行不通,但是您的HTML中有一个错误,该错误使#1
成为所有元素的父级,因此未选择任何内容.
Reverted to first revision again. I thought it did not work this way, but you have an error in your HTML which makes #1
parent of all elements, so none is selected.
<div class="container" id = "0" >
<div class="x" id = "1">
<div id = "2">
<p id = "3">
<span id = "4" >text</span>
</p>
<div> <!-- <-- must be a closing div tag -->
</div>
<div id="5">
<div id="6">
<p id="7">
<span class="x" id="8" >text</span>
<span id="9">text</span>
</p>
<div> <!-- <-- must be a closing div tag -->
</div>
<div> <!-- <-- must be a closing div tag -->
这篇关于jQuery:选择不是特定类后代的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文