Home  >  Article  >  Web Front-end  >  Usage examples of andSelf() method in jQuery_jquery

Usage examples of andSelf() method in jQuery_jquery

WBOY
WBOYOriginal
2016-05-16 16:21:30950browse

The example in this article describes 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:

Copy code The code is as follows:
.andSelf()

It may be difficult to understand this method, so let’s analyze the following example in detail. In order to facilitate viewing of the code, only the core content in the code has been intercepted.

Copy code The code is as follows:

$(".second").nextAll().css("color","green");

  • html area

  • DIV CSS Zone

  • Javascript Zone

  • Jquery Zone


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:

Copy code The code is as follows:

$(".second").nextAll().andSelf().css("color","green");

  • html area

  • DIV CSS Zone

  • Javascript Zone

  • Jquery Zone


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 to the current selection. If you start calling the css() method here, it will be the result of the first piece of code. If you call the andSelf() method, 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:

Copy code The code is as follows:






Script Home






  • html area

  • DIV CSS Zone

  • Javascript Zone

  • Jquery Zone





I hope this article will be helpful to everyone’s jQuery programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn