1.1

JQuery child selector expression

<div id="div">
    <div> <!-- first level -->
        <div> <!-- second level -->
            <div>1.1</div> <!-- third level -->
            <div>1.2</div>
        </div>
        <div>
            <div></div>
            <div>2.2</div>
        </div>
    </div>
</div>

What are the expressions of the jQuery selector to select the following elements:
1. div, commented on by the first level
2. Divas, commented on by the second level
3. Divas, commented on by the third level

+3
source share
5 answers

The key to all of them is either a selector >(child) or a method children().

First level:

$("#div > div")...
$("#div").children("div")...

Second level:

$("#div > div > div")...
$("#div").children("div").children("div")...

Third level:

$("#div > div > div > div")...
$("#div").children("div").children("div").children("div")...

(, div), children(). , :

$("#div").children().children()...
+5

> :

  • :
    • $("#div > div")
    • $("#div > *") ( )
  • :
    • $("#div > div > div")
    • $("#div > * > *") ( )
  • :
    • $("#div > div > div > div")
    • $("#div > * > * > *") ( )

.children() , :

$("#div").children()
$("#div").children().children()
$("#div").children().children().children()
+5

, CSS .

:

#div > *
#div > * > *
#div > * > * > *
+2
  • $('#div > *') - , #div. > #div, .

  • $('#div > * > *') - , # 1

  • $('#div > * > * > *')

+2

, ;

first level : $("#div").children("div:first-child")

Second level: $("#div:first-child").children("div:first-child").children("div:first-child")

Third level: $("#div").children("div":first-child).children("div:first-child").children("div:first-child")
0

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


All Articles