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.