如何在括号表示法访问的元素上使用jQuery方法? [英] How can I use jQuery methods on elements accessed by brackets notation?
本文介绍了如何在括号表示法访问的元素上使用jQuery方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以用括号表示法访问jQuery对象中的元素,如下所示:$('div')[0]
但是在这种情况下,无法使用jQuery方法.
Elements from jQuery object can be accessed by bracket notation, like so: $('div')[0]
But in this case jQuery methods cannot be used.
还有其他访问某些元素或使用jQuery方法的方法吗?
Any other way to access certain elements or use jQuery methods?
推荐答案
如果要获取集合中的第一个jQuery对象,请使用jQuery的eq()
方法:
If you're trying to get the first jQuery object in a collection, use jQuery's eq()
method:
$collection.eq(0);
首先创建集合时,也可以使用:eq()
选择器:
You can also use the :eq()
selector when creating the collection in the first place:
var $collection = $('div:eq(0)');
这篇关于如何在括号表示法访问的元素上使用jQuery方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文