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. ThanksWhat 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.
精彩评论