如何从项目列表中获取特定的jQuery项目? [英] How to get a specific jQuery item from a list of items?

查看:140
本文介绍了如何从项目列表中获取特定的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

这篇关于如何从项目列表中获取特定的jQuery项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆