jquery supports the eq method; this method can return the element with the specified index number of the selected element. The index number of the element starts from 0, and the syntax is "element object.eq(index)"; within the method The parameter is used to specify the index of the element, which can be set to an integer or a negative number. When the parameter is set to a negative number, the index will be calculated from the end of the selected element.
The operating environment of this tutorial: windows10 system, jquery3.6.0 version, Dell G3 computer.
The eq() method returns the element with the specified index number of the selected element.
Index numbers start with 0, so the index number of the first element is 0 (not 1).
Syntax
$(selector).eq(index)
Parameter Description
index Required. Specifies the index of the element. Can be an integer or negative number.
Note: Using negative numbers will calculate the index from the end of the selected element.
Example:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script src="js/jquery.min.js"></script> <script> $(function () { $("li").eq(3).css("color", "red"); }) </script> </head> <body> <ul> <li>HTML</li> <li>CSS</li> <li>JavaScript</li> <li>jQuery</li> <li>Vue.js</li> </ul> </body> </html>
The preview effect is as shown in the figure
The subscript of the eq() method starts from 0, and the The index of 1 li element is 0, the index of 2nd li element is 1,..., the index of nth element is n-1. Therefore, $("li").eq(3) means selecting the 4th li element.
$("li").eq(3).css("color", "red");
In fact, the eq() method is very similar to the :eq() selector. The above code can be equivalent to:
$("li:eq(3)").css("color", "red");
Friends will ask: "There is obviously an :eq() selector, why do we need to create an eq() method?" In fact, the form of the selector is fixed, and in some cases the effect will be poor, and filtering Methods allow us to manipulate elements more flexibly. In other words: the filtering method is actually a supplement to the selector
Video tutorial recommendation:jQuery video tutorial
The above is the detailed content of Does jquery not support eq method?. For more information, please follow other related articles on the PHP Chinese website!