仅选择第一级元素,而不选择具有相同元素名称的子元素 [英] Selecting only first level element, not child elements with the same element name
本文介绍了仅选择第一级元素,而不选择具有相同元素名称的子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何选择只有第一个 级别.block而不是任何子级?
How would I select only the first level .block and not any of the children?
$ '.block:not(Children of this here)')
< -
<div class="block"> <!-- this -->
<div class="block"> <!-- not this -->
<div class="block"> <!-- not this -->
</div>
</div>
</div>
<div class="block"> <!-- and this -->
<div class="block"> <!-- not this -->
<div class="block"> <!-- not this -->
</div>
</div>
</div>
推荐答案
如果示例标记有父元素下面。如果没有,父项将是 body
(如果它是有效的HTML)。
If that sample markup has a parent element, for example below. If not, the parent will be body
if it is valid HTML.
<div id="parent">
<div class="block">
<div class="block">
<div class="block">
</div>
</div>
</div>
</div>
jQuery
jQuery
$('#parent > .block').css({ border: '1px solid red' });
这篇关于仅选择第一级元素,而不选择具有相同元素名称的子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文