Example of the use of the andSelf () method in jQuery, jqueryandself

Source: Internet
Author: User

Example of the use of the andSelf () method in jQuery, jqueryandself

This document describes the use of the andSelf () method in jQuery. Share it with you for your reference. The specific analysis is as follows:

This method can add previously selected elements to the current element set.

Syntax structure:
Copy codeThe Code is as follows:. andSelf ()
This method may be difficult to understand, so we will analyze the example below in detail. In order to facilitate viewing the code, we will only intercept the core content in the code.

Copy codeThe Code is as follows:
$ (". Second" ).nextAll().css ("color", "green ");
<Ul>
<Li> html zone </li>
<Li class = "second"> DIV + CSS area </li>
<Li> Javascript area </li>
<Li> Jquery area </li>
</Ul>

After the above Code is run, the text color in the third and fourth li is set to green. Let's look at the following code:

Copy codeThe Code is as follows:
$ (". Second" ).nextAll().andSelf().css ("color", "green ");
<Ul>
<Li> html zone </li>
<Li class = "second"> DIV + CSS area </li>
<Li> Javascript area </li>
<Li> Jquery area </li>
</Ul>

After the above Code is run, the text color in the second, third, and fourth li is set to green.
The difference between the two code running results is caused by the andSelf () method.

Analyze the code execution process: first, the class selector selects the second li element, and then the nextAll () method converts the third and fourth elements to the selected one. If you start to call the css () method, it is the result of the first code. If you call the andSelf () method, the previously selected elements will be added to the current element set, and then the css () method will be called. The font color of the three li elements will change to green.

Instance code:

Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Meta name = "author" content = "http://www.bkjia.com/"/>
<Title> helping customers </title>
<Script type = "text/javascript" src = "mytest/jQuery/jquery-1.8.3.js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
$ (". Second" ).nextAll().andSelf().css ("color", "green ");
})
</Script>
</Head>
<Body>
<Div>
<Ul>
<Li> html zone </li>
<Li class = "second"> DIV + CSS area </li>
<Li> Javascript area </li>
<Li> Jquery area </li>
</Ul>
</Div>
</Body>
</Html>

I hope this article will help you with jQuery programming.

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.