When ...">

JQuery children selector explanation

I have this markup

<div id="parent">
    <div class="child"></div>
    <div class="child"></div>
</div>

When I select child with jQuery, there are two ways:

$("#parent .child")

and

$("#parent").find(".child")

Can someone explain the difference between the two selectors and which one is better?

+4
source share
2 answers

$("#parent .child")and $("#parent").find(".child")match. Both will select all children with the class name childrecursively. those. it returns children of any level.

If you use $("#parent > .child"), it will only return first level children. This selector is the same as$("#parent").children(".child")

0
source

. $("#parent .child") $("#parent").find(".child") ( div child div parent.

0

Source: https://habr.com/ru/post/1613496/


All Articles