这有多少元素?

Jan*_*ven 3 javascript jquery

我怎么知道一个元素是多么神圣?(使用Javascript或jQuery)假设我有一个带有多个LI的UL,我怎么知道LI点击的是第4个还是第6个?

$('li').click(function(){
    nth = $(this).nth();
    alert(nth);
});
Run Code Online (Sandbox Code Playgroud)

Ale*_*pin 6

你可以使用jQuery .index.

?$("li").click(function() {
    var nth = $("ul li").index($(this));
    alert(nth);
});?
Run Code Online (Sandbox Code Playgroud)

实例