Home > Web Front-end > Front-end Q&A > Does jquery not support eq method?

Does jquery not support eq method?

WBOY
Release: 2022-06-24 17:22:13
Original
1423 people have browsed it

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.

Does jquery not support eq method?

The operating environment of this tutorial: windows10 system, jquery3.6.0 version, Dell G3 computer.

jquery supports the eq method

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)
Copy after login

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>
Copy after login

The preview effect is as shown in the figure

Does jquery not support eq method?

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");
Copy after login

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");
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template