jquery selector Gets the parent element, sibling element, child element

Source: Internet
Author: User

First, get the parent element

1. parent ([expr]):

Gets all the parent elements of the specified element

<div id= "Par_div" ><a id= "Href_fir" href= "#" >href_fir</a>
<a id= "Href_sec" href= "#" >href_sec</a>
<a id= "Href_thr" href= "#" >href_thr</a></div>
<span id= "Par_span" >
<a id= "Href_fiv" href= "#" >href_fiv</a>
</span>$ (document). Ready (function () {
$ ("a"). Parent (). addclass (' A_par ');
});

second, get the sibling elements:1. Next ([expr]):

Gets the next sibling element of the specified element (note the next sibling element, OH)

<! DOCTYPE html>
<script type= "Text/javascript" src= "/jquery/jquery.js" ></script>

<body>
<ul>
<li>list Item 1</li>
<li>list Item 2</li>
<li class= "Third-item" >list item 3</li>
<li>list Item 4</li>
<li>list Item 5</li>
</ul>

<script>
$ (' Li.third-item '). Next (). CSS (' background-color ', ' red ');
</script>

</body>

The result of this example is that only the list item 4 background color turns red

2. Nextall ([expr]):

Gets all sibling elements behind the specified element

Hello

Hello Again
<div><span>and Again</span></div>var P_nex = $ ("P"). Nextall ();
P_nex.addclass (' P_next_all ');


Pay attention to the last "<p&gt" label Oh, also added to the ' P_next_all ' this class name Oh ~ ~

3, Andself ():

Gets all the sibling elements behind the specified element, followed by the specified element

I feel this function is the most interesting function, what does it mean? The literal translation comes from "Still Have Me", "also have oneself", yes, also have oneself.

<p>hello</p><p>hello Again</p><div><span>and Again</span></div> var P_nex = $ ("P"). Nextall (). Andself ();
P_nex.addclass (' P_next_all ');

Pay attention to the first "<p>" tag ah, this sentence means to choose the P tag after all the sibling tags, as well as their own ...

The following two do not cite specific examples, which is actually the opposite of next () and Nextall ()

4, prev (): Gets the previous sibling element of the specified element (last OH).

5, Prevall (): Gets all sibling elements in front of the specified element.

Third, get child elements

1. Find child element mode 1:>

For example: var anods = $ ("ul > a"); Find all a labels under UL

2. Find child element Mode 2:children ()

3. Find child element Mode 3:find ()

Here is a brief introduction to the similarities and differences between the following children () and find ():

The 1> children and find methods are used to obtain the sub-elements of element, neither of which will return text node, just like most jquery methods.
The 2> children method obtains only the child element of a subordinate element, namely: immediate children.
The 3> Find method obtains all subordinate elements, namely: descendants of these elements in the DOM tree
The parameter selector of the 4> children method is optional (optionally), which is used to filter the child elements,

But the parameter selector method of the Find method is required.
The 5> Find method can actually be implemented by using JQuery (selector, context). That is $ (' li.item-ii '). Find (' Li ') is equivalent to $ (' Li ', ' Li.item-ii ').

Cases:

<ul class= "Level-1" >
<li class= "Item-i" >I</li>
<li class= "Item-ii" >ii
<ul class= "Level-2" >
<li class= "Item-a" >A</li>
<li class= "Item-b" >b
<ul class= "Level-3" >
<li class= "Item-1" >1</li>
<li class= "Item-2" >2</li>
<li class= "Item-3" >3</li>
</ul>
</li>
<li class= "Item-c" >C</li>
</ul>
</li>
<li class= "ITEM-III" >III</li>
</ul>
Use: $ (' Ul.level-2 '). Children (). CSS (' border ', ' 1px solid green '); The effect is:


Use $ (' Ul.level-2 '). Find (' Li '). CSS (' border ', ' 1px solid green '); The effect is:

jquery selector Gets the parent element, sibling element, child element

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.