开发者

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

<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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜