Home > Web Front-end > Front-end Q&A > What is the selector eq in jquery

What is the selector eq in jquery

PHPz
Release: 2023-05-14 10:13:07
Original
1080 people have browsed it

jQuery is a JavaScript library designed to simplify HTML document traversal and manipulation, event handling, animation effects, and AJAX interaction. The jQuery selector is the most basic and core part of the jQuery library. It is the function used by the jQuery library to find DOM elements.

In the jQuery selector, eq() is an index-based traversal and filtering selector. It is used to select elements at specific index positions in one or more elements to achieve a collection of elements. Sift and filter.

Below, we will introduce in detail the usage and operation techniques of jQuery selector eq().

1. The syntax of the jQuery selector eq()

In jQuery, the syntax of the eq() selector is as follows:

$(selector).eq( index)

Among them, $(selector) is jQuery's selector expression, used to specify the elements that need to be filtered and selected. Index is an integer value starting from 0, used to specify the index position of the element to be selected.

It should be noted that when index is less than 0 or greater than the number of elements minus one, eq() will return an empty jQuery object instead of reporting an error.

2. Examples of jQuery selector eq()

The following will introduce some practical application examples of jQuery selector eq() in order to better understand its specific usage and operation. Skill.

Example 1: Selector filtering

In the following example, we use three different selector filtering methods to select the li elements in the list, and then use eq(1) to select The selector selects the second element to change the background color.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>jQuery demo - eq()选择器</title>
    <style type="text/css">
        .active {
            background-color: #EE9A00;
        }
    </style>
</head>
<body>
    <ul>
        <li>Apple</li>
        <li>Banana</li>
        <li>Pineapple</li>
        <li>Orange</li>
        <li>Grape</li>
    </ul>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <script type="text/javascript">
        // 根据元素名筛选
        $('li').eq(1).addClass('active');  
        // 根据类名筛选
        $('.active').eq(1).addClass('active');  
         // 根据ID名筛选
        $('#word3').eq(0).addClass('active');  
    </script>
</body>
</html>
Copy after login

Example 2: Selector traversal and operation

In the following example, we use the selector eq() to implement traversal of multiple input box elements in a form And operations, when we enter an operation in any input box, the values ​​of other input boxes will also change accordingly.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>jQuery demo - eq()选择器</title>
</head>
<body>
    <form>
        <input type="text" id="input1" value="default value"/>
        <input type="text" id="input2" value="default value"/>
        <input type="text" id="input3" value="default value"/>
        <input type="text" id="input4" value="default value"/>
    </form>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <script type="text/javascript">
        $("input").on("input", function() {
            var index = $("input").index(this);
            $("input").eq(index).val($(this).val());
        }); 
    </script>
</body>
</html>
Copy after login

3. Summary

Through the above examples, we can see the important role of the eq() selector in jQuery. Using it, we can quickly and accurately locate, filter and operate the elements we need, thereby achieving more efficient and convenient programming and development. Therefore, when learning and mastering the jQuery selector, we must focus on mastering the usage and operation skills of the eq() selector, which is of great significance to improving our programming efficiency and work quality.

The above is the detailed content of What is the selector eq in jquery. For more information, please follow other related articles on the PHP Chinese website!

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