Home >Web Front-end >JS Tutorial >Usage examples of andSelf() method in jQuery
This article mainly introduces the usage of the andSelf() method in jQuery. The example analyzes the usage skills of the andSelf() method to add previously selected elements to the current element collection. Friends in need can refer to it. The following
examples in this article describe the usage of the andSelf() method in jQuery. Share it with everyone for your reference. The specific analysis is as follows:
This method can add the previously selected element to the current element collection.
Grammar structure:
The code is as follows:
.andSelf()
It is possible for this method It is difficult to understand, so let’s analyze the following example in detail. In order to facilitate viewing of the code, only the core content of the code has been intercepted.
The code is as follows:
$(".second"). next All().css("color","green"); <ul> <li>html专区</li> <li class="second">p+CSS专区</li> <li> Javascript 专区</li> <li>Jquery专区</li> </ul>
After the above code is run, the text color in the third and fourth li is set to green. Look at the following code again:
The code is as follows:
$(".second").nextAll().andSelf().css("color","green"); <ul> <li>html专区</li> <li class="second">p+CSS专区</li> <li>Javascript专区</li> <li>Jquery专区</li> </ul>
After the above code is run, the text color in the second, third and fourth li is set to green.
The difference in the running results of the two codes is due to the andSelf() method.
Analyze the execution process of the code: first use the class selector to select the second li element, and then use the nextAll() method to convert the third and fourth elements. is currently selected. If you start calling the css() method here, it will be the result of the first piece of code. If the andSelf() method is called, the previously selected element will be added to the current element collection, and then the css() method is called, and the font color in the three li elements will turn green.
Example code:
The code is as follows:
<!DOCTYPE html> <html> < head > <meta charset=" utf-8"> <meta name="author" content="http://www.jb51.net/" /> <title>脚本之家</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> <p> <ul> <li>html专区</li> <li class="second">p+CSS专区</li> <li>Javascript专区</li> <li>Jquery专区</li> </ul> </p> </body> </html>
The above is the detailed content of Usage examples of andSelf() method in jQuery. For more information, please follow other related articles on the PHP Chinese website!