About parent and parents of jquery
A parent is a set of elements that obtain a unique parent element that contains all matching elements.
Parents is a set of elements (excluding the root element) that contain the ancestor elements of all matching elements ). You can use an optional expression to filter data.
It can be seen that parent is clear, that is, the parent element of the current element; parents is the ancestor element of the current element. The following are examples:
<Div
Id = 'did1'>
<Div id = 'div2'> <p> </P> </div>
<Div
Id = 'did3' class = 'A'> <p> </P> </div>
<Div
Id = 'div4 '> <p> </P> </div>
</Div>
$ ('P'). Parent () obtains div2, div3, div4
$ ('P'). Parent ('. A') obtains div3.
$ ('P'). Parent (). Parent () gets div1, which is odd. However, the characteristics of jquery objects determine that this is feasible.
$ ('P'). Parents () obtains div1, div2, div3, and div4.
$ ('P'). Parents ('. A') obtains div3.
Jquery's selector is really powerful. I think jquery's most comfortable to use is its selector and filtering function.
- <
Html
>
- <
Head
>
</
Head
>
- <
Body
>
- <
Div
ID
= "D1"
>
- <
P
>
Hello
- <
Div
ID
= "CRD"
>
XXXXX </
Div
>
- </
P
>
- </
Div
>
- <
Div
Class
= "Selected"
>
- <
P
>
Hello again </
P
>
- </
Div
>
- </
Body
>
- </
Html
>
- Parent ([expr])
- Note:
- Expr (string): (optional) filtering expression
- Obtains a set of elements that contain the unique parent element of all matching elements.
- Example:
- <
Pre
Class
= JS
Name
= "Code"
>
$ ("P"). Parent ()
- </
Pre
>
- <
BR
>
Result:
- <
BR
>
<
Pre
Class
= Html
Name
= "Code"
>
<
Div
ID
= "D1"
>
</
Div
>
<
Div
Class
= "Selected"
>
</
Div
>
</
Pre
>
- <
BR
>
- <
BR
>
Parents
([Expr])
- <
BR
>
Note:
- <
BR
>
Obtains a set of elements (excluding the root element) that contain all the ancestor elements matching the elements ). You can use an optional expression to filter data.
- <
BR
>
- <
BR
>
It mainly refers to the understanding of ancestor elements.
- <
BR
>
- <
BR
>
<
Pre
Class
= JS
Name
= "Code"
>
</
Pre
>
- <
BR
>
$ ("P"). Parents
()
- <
BR
>
<HTML>
$ ("P"). Parent ()
$("p").parent()Result:
HTML code
- <
Div
ID
= "D1"
>
</
Div
>
<
Div
Class
= "Selected"
>
</
Div
>
<div id="d1"></div><div class="selected"></div>
Parents
([Expr])
Note:
Obtains a set of elements (excluding the root element) that contain all the ancestor elements matching the elements ). You can use an optional expression to filter data.
It mainly refers to the understanding of ancestor elements.
JS Code
$ ("P"). Parents
()
Result: div, body, HTML, and <Div
Class = "selected"> </div> element.