开发者

jquery select all elements in just the current element and not child element

开发者 https://www.devze.com 2023-04-12 19:35 出处:网络
I\'ve done a few things such as selecting all children of an element, all parents of an element, first child etc.. but this is a little tricky.

I've done a few things such as selecting all children of an element, all parents of an element, first child etc.. but this is a little tricky.

I have the structure somewhat similar to this:

<div class="body">
    <a href="something">Something</a>
    <small>
          <a href="something-else开发者_运维技巧">Something-else</a>
    </small>
</div>

and this structure is repeated a whole lotta times.

I need to select all <a> elements found only in the top level div.body and ignore all <a> that's found in the <small> element.

How do I go around with this please?

thanks,.


Simply use the child selector rather than the descendant selector:

$('div.body > a');


The child selector only selection direct elements of a parent element, so:

$('div.body > a')

should do the trick


Use the child selector:

$("div.body > a")

A > B means: "B is a immediate child of A", whereas
A B means: `B is a (not strictly immediate) child of A.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号