jQuery | index() 示例
index()是 jQuery 中的一个内置方法,用于返回与选择器相关的指定元素的索引。
句法:
$(selector).index(element)
参数:它接受一个可选参数“元素”,用于获取元素的位置。
返回值:它返回一个整数,表示指定元素的索引。
代码#1:
Click on the elements of the list to display their index
number with respect to the other elements in the list.
- Geeks
- for
- Geeks
输出:
在单击列表中的任何元素之前
Click on the elements of the list to display their index number with
respect to the other elements in the list.
- 极客
- 为了
- 极客
点击“for”后——
Click on the elements of the list to display their index number with
respect with the other elements in the list.
- 极客
- 为了
- 极客
代码#2:
Click on the elements of the list to display their index
number with respect with the other elements in the list.
- Geeks
- for
- Geeks
输出:
在单击列表中的任何元素之前 -
Click on the elements of the list to display their index number with
respect with the other elements in the list.
- 极客
- 为了
- 极客
单击列表中的任何元素后-
Click on the elements of the list to display their index number with
respect with the other elements in the list.
- 极客
- 为了
- 极客