Home >Web Front-end >Front-end Q&A >How to determine which number is selected in jquery
In jquery, you can use the index() method to determine which element the selected element is. This method is used to return the index position of the specified element relative to other specified elements. The syntax is "specified element object.index" (this)".
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
How does jquery determine which number is selected
Suppose there is the following HTML code:
<ul> <li>jQuery判断当前元素是第几个元素示例</li> <li>jQuery获取第N个元素示例</li> <li>jQuery选择器示例</li></ul>
jQuery judgment Which element is the current element?
If we click on any li tag and want to know which li tag is currently clicked, we can use the following code:
$("ul li").click(function () { var index = $("ul li").index(this); alert(index); });
Such as the jQuery above Code, if you click on the first one, it will prompt "0", if it is the second li tag, it will prompt "1", please note that the index sequence number starts from 0.
index() method returns the index position of the specified element relative to other specified elements.
These elements can be specified via jQuery selectors or DOM elements.
Note: If the element is not found, index() will return -1.
Recommended related video tutorials: jQuery video tutorial
The above is the detailed content of How to determine which number is selected in jquery. For more information, please follow other related articles on the PHP Chinese website!