仅选择第一级元素,而不选择具有相同元素名称的子元素

tes*_*ter 6 css jquery

我如何选择第一级.block而不选择任何一个孩子?

$('.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>
Run Code Online (Sandbox Code Playgroud)

ale*_*lex 9

如果该样本标记具有父元素,例如下面.如果不是,则父级将body是有效的HTML.

HTML

<div id="parent">
<div class="block">
  <div class="block">
    <div class="block">

    </div>
  </div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)

jQuery的

$('#parent > .block').css({ border: '1px solid red' });
Run Code Online (Sandbox Code Playgroud)