开发者

How to get a specific jQuery item from a list of items?

I have this:

<ul>
    <li>firs开发者_如何学编程t</li>
    <li>second</li>
    <li>third</li>
    <li>fourth</li>
</ul>

Then I select it all with jQuery: $('ul').find('li'); or $('ul li');

How can I, from those two jQuery selectors get the, for instance only second li, or third, and to leave first and fourt alone?

I thought it might work with:

$('myselector').get(indexNumber); // however, it won't work.

Any ideas for this issue? Thanks.


The get method returns the DOM element, so then you would have to wrap it inside a new jQuery object.

You can use the eq method:

var j = $('ul li').eq(1); // gets the second list item


Use :eq() Selector. For for example, for second element use:

 $("ul li:eq(1)"); 


I would try:

$("ul li:nth-child(2)")


$('li').get(0) will return plain DOM element. you cannot call jQuery methods on same.


you can use nth-child

$("ul li:nth-child(2)") //this will select second child because it is 1 based index

here is a fiddle http://jsfiddle.net/xyyWh/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜