如何从项目列表中获取特定的jQuery项目?
问题描述:
我有这个:
<ul>
<li>first</li>
<li>second</li>
<li>third</li>
<li>fourth</li>
</ul>
然后我用jQuery选择它: $('ul')。 find('li'); 或 $('ul li');
Then I select it all with jQuery: $('ul').find('li'); or $('ul li');
我怎么能从这两个jQuery选择器获得,例如只有第二个li或第三个,并离开first和fourt单独?
How can I, from those two jQuery selectors get the, for instance only second li, or third, and to leave first and fourt alone?
我认为它可能工作与:
$('myselector').get(indexNumber); // however, it won't work.
有关此问题的任何想法吗?
Any ideas for this issue? Thanks.
答
get 方法返回DOM元素,你必须将它包装到一个新的jQuery对象中。
The get method returns the DOM element, so then you would have to wrap it inside a new jQuery object.
你可以使用 eq 方法:
var j = $('ul li').eq(1); // gets the second list item