Get the App
SLTechnology News&Howtos  ›  Development  › 

How does jquery judge which one is chosen?

Shulou Source: shulou.com Published: 2022-06-02 03:32:49 09月28日 Update

In this article, the editor introduces in detail "jquery how to judge how to be selected", detailed content, clear steps, and proper handling of details. I hope this article "jquery how to judge how to be selected is the first one" can help you solve your doubts.

In jquery, you can use the index () method to determine which element is selected, which is used to return the index position of the specified element relative to other specified elements, with the syntax of "specified element object .index (this)".

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

How does jquery judge which one is chosen?

Suppose you have a piece of HTML code like this:

JQuery determines the number of elements the current element is example jQuery gets the Nth element example jQuery selector example

JQuery determines which element the current element is.

If we click on any of the li tags and want to know which li tag we are clicking on, we can use the following code:

$("ul li") .click (function () {var index = $("ul li") .index (this); alert (index);})

Such as the above jQuery code, if the first click will prompt "0", if it is the second li tag will prompt "1", note that the index serial number starts at 0.

The index () method returns the index position of the specified element relative to other specified elements.

These elements can be specified through jQuery selectors or DOM elements.

Note: if no element is found, index () returns-1.

After reading this, the article "how to judge which jquery is selected" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Tags: Elements code articles methods tags examples locations content facets prompts selections appropriateness such as above objects sequences serial numbers ideas tutorials new knowledge more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Huawei Redmi Shulou Information Apple