如何使用 jquery select 不是特定元素 child 的元素?
How to select element that isn't a child of specific element using jquery?
我如何才能 select 只有没有 div.option
作为 parent 的 span
?
<div class="option">
<span>Content goes here</span>
</div>
<span>Content goes here</span>
<span>Content goes here</span>
<div class="option">
<span>Content goes here</span>
</div>
您需要 select 所有 span
并使用 :not()
排除 .option
的子跨度
$("span:not(div.option > span)").css("color", "red");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="option">
<span>Content goes here</span>
</div>
<span>Content goes here</span>
<span>Content goes here</span>
<div class="option">
<span>Content goes here</span>
</div>
我如何才能 select 只有没有 div.option
作为 parent 的 span
?
<div class="option">
<span>Content goes here</span>
</div>
<span>Content goes here</span>
<span>Content goes here</span>
<div class="option">
<span>Content goes here</span>
</div>
您需要 select 所有 span
并使用 :not()
排除 .option
$("span:not(div.option > span)").css("color", "red");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="option">
<span>Content goes here</span>
</div>
<span>Content goes here</span>
<span>Content goes here</span>
<div class="option">
<span>Content goes here</span>
</div>