开发者

How to check the serial number of element that have specific class using jquery?

开发者 https://www.devze.com 2023-02-03 19:21 出处:网络
<ul> <li class=\"res\"></li> <li class=\"res\"></li> <li class=\"res selected\"></li>
<ul>
    <li class="res"></li>
    <li class="res"></li>
    <li class="res selected"></li>
    <li class="res"></li>
</ul>

Using $('ul li.selected').length I can check total number of "selected elements" BUT I'd l开发者_StackOverflowike to check the serial number of the "selected element" (in that case 3)

Can you help me do that.

Thanks


What exactly do you mean by serial number?

The content?

 alert (  $('ul li.selected').text() );

The index?

 var listItem = $('ul li.selected');
 alert('Index: ' + $('ul li').index(listItem));


You can use the index() method like this:

alert('Index: ' + $('li').index('.selected'));


Try

$("ul li.selected").index() + 1

Since .index() is 0 based you need to add 1 to get your desired result.

0

精彩评论

暂无评论...
验证码 换一张
取 消