Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use eq () of jquery

Shulou Source: shulou.com Published: 2022-06-01 09:35:54 10月04日 Update

This article mainly introduces "how to use eq () of jquery". In daily operation, I believe many people have doubts about how to use eq () of jquery. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to use eq () of jquery"! Next, please follow the editor to study!

In jquery, the eq () method is used to return the element with the specified index number of the selected element, the syntax "$(selector) .eq (index)", while the ": eq ()" selector is used to select the element with the specified index value, and the syntax "$(": eq (index) ")".

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

In jquery, eq () is divided into two situations:

Eq () method

Eq () selector

Jquery eq () method

The eq () method returns the element with the specified index number of the selected element. The index number starts with 0, so the index number of the first element is 0 (not 1).

Syntax:

$(selector) .eq (index)

Index: required. Specifies the index of the element, which can be integer or negative. If you use a negative number, the index is calculated starting at the end of the selected element.

Note: the index number (index) starts at 0, so the index number of the first element is 0 (not 1).

Example 1: select the second

Element (index number is 1)

$(document) .ready (function () {$("p"). Eq (1). Css ("background-color", "yellow");}); Welcome to my home page

My name is Donald (subscript 0).

Donald Duck (index 1).

I live in Duckburg (index 2).

My best friend is Mickey (index 3).

Example 2: use a negative number to return the second from the end of the selected element

element.

$(document) .ready (function () {$("p"). Eq (- 2). Css ("background-color", "yellow");}); Welcome to my home page

My name is Donald.

Donald Duck .

I live in Duckburg (will be selected because it is the penultimate element).

My best friend is Mickey.

Jquery: eq () selector

The eq () selector selects the element with the specified index value.

The index value starts at 0, so the index value of the first element is 0 (not 1).

The most common usage: use with other selectors to select the elements of the specified index in the specified combination.

Syntax:

$(": eq (index)")

Index: required. Specifies the index of the element.

Example: select the second

Elements:

$(document) .ready (function () {$("p:eq (1)") .css ("background-color", "yellow");}); Welcome to my home page

My name is Donald.

Donald Duck .

I live in Duckburg.

My best friend is Mickey.

At this point, the study of "how to use eq () of jquery" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Element index index number method selection grammar learning home page name best friends examples negative numbers living beginning more help practical next subscript Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Docker Huawei Apple Shulou Information