jQuery - 从数组中获取元素作为jQuery元素? [英] jQuery - Get element from array as jQuery element?

查看:771
本文介绍了jQuery - 从数组中获取元素作为jQuery元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我打电话

$(".myClass")

我得到一个元素数组。如果我现在想把第一个元素作为jquery元素,我会做这样的事情:

I get an array of elements. If I now want to get the first element as jquery element I would do something like this:

$($(".myClass").get(0))

所以我包装DOM-Element,我从使用jQuery运算符再次使用数组。有没有更优雅的方式来做到这一点?一些get方法,例如返回一个jQuery元素?

So I wrap the DOM-Element, which I get from the array again with the jQuery operator. Is there a more elegant way to do this? Some get method, which returns a jQuery element for example?

推荐答案

使用 eq()方法

$(".myClass").eq(0)

这将返回一个jQuery对象,而 .get()返回一个DOM元素。

This returns a jQuery object, whereas .get() returns a DOM element.

.eq()允许你指定索引,但是如果你只想要第一个你可以使用 .first(),或者如果你只想要最后一个你可以使用(惊喜!) .last()

.eq() lets you specify the index, but if you just want the first you can use .first(), or if you just want the last you can use (surprise!) .last().


我得到了一个元素数组。

不,你没有得到一个像数组一样的jQuery对象对象,而不是实际的数组。

No you don't, you get a jQuery object which is an array-like object, not an actual array.

如果您计划使用jQuery,我建议花半小时浏览所有方法的清单

If you plan to use jQuery much I suggest spending half an hour browsing through the list of all methods.

这篇关于jQuery - 从数组中获取元素作为jQuery元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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