How to get the elements selected by eq in jquery

WBOY
Release: 2022-06-09 17:27:34
Original
1372 people have browsed it

In jquery, you can use the ":not()" selector to obtain the elements selected by the eq selector. The ":not()" selector is used to select all elements except the specified element. The most common It is used together with other selectors, and the syntax is "$("Element:not(:eq(...))").Method".

How to get the elements selected by eq in jquery

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

How to get the elements selected by jquery except eq

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

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

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

index values ​​start from 0, and the index value of all first elements is 0 (not 1).

is often used with other elements/selectors to select elements with a specific sequence number in a specified group (such as the example above).

Syntax

$(":eq(index)")
Copy after login

Parameter Description

index Required. Specifies the index value of the element.

If you want to get the elements selected except eq, just use the :not() selector.

: The not() selector selects all elements except the specified element.

The most common usage: used with other selectors to select all elements in the specified combination except the specified element (such as the example above).

Syntax

$(":not(selector)")
Copy after login

Parameter Description

selector Required. Specifies elements that are not selected.

This parameter accepts any type of selector.

The example is as follows:

<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript"> 
 
$(document).ready(function(){
    $("p:eq(1)").css("background-color","#B2E0FF");
});
 
</script>
 
</head>
<body>
<html>
<body>
<h1>Welcome to My Homepage</h1>
<p class="intro">My name is Donald</p>
<p>I live in Duckburg</p>
<p>My best friend is Mickey</p>
<div id="choose">
Who is your favourite:
<ul>
<li>Goofy</li>
<li>Mickey</li>
<li>Pluto</li>
</ul>
</div>
</body>
</html>
 
 
</body>
</html>
Copy after login

Output result:

How to get the elements selected by eq in jquery

The example of using the not selector is as follows:

<script type="text/javascript"> 
 
$(document).ready(function(){
    $("p:not(:eq(1))").css("background-color","#B2E0FF");
});
 
</script>	
Copy after login

Output result :

How to get the elements selected by eq in jquery

Video tutorial recommendation: jQuery video tutorial

The above is the detailed content of How to get the elements selected by eq in jquery. 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