开发者

Selector to select third list item?

开发者 https://www.devze.com 2022-12-24 02:22 出处:网络
Suppose a HTML markup like this: <ul> <li>List item 1</li> <li>List item 2</li>

Suppose a HTML markup like this:

<ul>
    <li>List item 1</li>
    <li>List item 2</li>
    <li>List item 3</li><!-- I want to select only this -->
</ul>

Would it be possible to select only the third list item in the unord开发者_运维问答ered list element? I've been browsing the jQuery documentation but I can't find any selector that could do this.


You may want to use the nth-child selector

$('ul li:nth-child(3)')


$("ul li:eq(2)") // 3rd item

http://api.jquery.com/eq-selector/


here you go

$('ul>li').each(function(i){
    if(i == 2)
    {
       //your code goes here

       return;
    } 
});

cheers

0

精彩评论

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