Summary of several methods for getting elements in jquery

Source: Internet
Author: User

1. Obtain elements from the set by the specified sequence number
Html:
Copy codeThe Code is as follows:
<Div>
<P> 0 </p>
<P> 1 </p>
<P> 2 </p>
<P> 3 </p>
<P> 4 </p>
<P> 5 </p>
<P> 6 </p>
<P> 7 </p>
</Div>

JS
Copy codeThe Code is as follows:
<Script type = "text/javascript">
JQuery (function (){
$ ("P" ).eq(2).css ("color", "red ");
$ ("P" ).eq(32.16.css ("color", "red ");
})
</Script>

2. Obtain the elements with the same conditions and the specified range.
Html:
Copy codeThe Code is as follows:
<Div>
<P> 0 </p>
<P> 1 </p>
<P class = "center"> 2 </p>
<P class = "center"> 3 </p>
<P> 4 </p>
<P> 5 </p>
<P> 6 </p>
<P> 7 </p>
</Div>

JS
Copy codeThe Code is as follows:
<Script type = "text/javascript">
JQuery (function (){
$ ("P" ).filter('.center').css ("color", "red ");
})
$ (Function (){
$ ("P" ).slice(5,72.16.css ("color", "yellow ");
})
</Script>

3. Obtain elements consistent with the conditional expression.
Html:
Copy codeThe Code is as follows:
<Div>
<P> 0 </p>
<P> 1 </p>
<P class = "center"> 2 </p>
<P class = "center"> 3 </p>
<P> 4 </p>
<P class = "aa"> 5 </p>
<P> 6 </p>
<P> 7 </p>
</Div>

Js
Copy codeThe Code is as follows:
<Script type = "text/javascript">
JQuery (function (){
$ ("P"). each (function (){
Switch (true ){
Case $ (this). is (". center "):
((This).css ("color", "red ");
Break;
Case $ (this). is (". aa "):
((This).css ("color", "yellow ");
Break;
}
})
})
</Script>

4. Obtain the previous and next elements of an element.
Html:
Copy codeThe Code is as follows:
<Div id = "aa">
<P> 1 </p>
<P class = "yes"> NO. 2 </p>
<P> NO. 3 </p>
<P> 4 </p>
<P> No. 5 </p>
<P class = "yes"> No. 6 </p>
<P class = "yes"> 7 </p>
</Div>

Js
Copy codeThe Code is as follows:
// Obtain the next element of the element
JQuery (function (){
$ ("P"). next (". yes" ).css ("color", "red ");
})
// Obtain the previous element of the element
JQuery (function (){
$ ("P"). prev (". yes" ).css ("color", "red ");
})

5. Obtain the parent and child elements of an element.
Html:
Copy codeThe Code is as follows:
<Div id = "aa">
<P> 1 </p>
<P class = "yes"> NO. 2 </p>
<P> NO. 3 </p>
<P> 4 </p>
<P> No. 5 </p>
<P class = "yes"> No. 6 </p>
<P class = "yes"> 7 </p>
</Div>

Js
Copy codeThe Code is as follows:
// Obtain the parent element of an element
JQuery (function (){
$ ("P" ).parent().css ("color", "red ");
})
// Obtain the child element of the element
JQuery (function (){
$ ("# Aa"). children (". yes" ).css ("color", "red ");
})

Related Article

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.