获取数组中对象的属性
由于某种原因,当我定义数组中的数字时,jQuery 没有给我数组中对象的属性。但是,当没有特别定义时,它将返回数组中第一个对象的类。例如,这个可以工作:
$('#content').prepend($('div #left ol.group li',data).attr("class"));
但这个不行:
$('#content').prepend($('div #left ol.group li',data)[3].attr("class"));
有人可以向我解释为什么它不像大多数其他功能那样工作以及如何使其工作吗?
For some reason, jQuery isn't giving me the attributes of objects in array when I define the number in the array. It will return the class of the first object in an array when none in particular are defined, though. For example, this works:
$('#content').prepend($('div #left ol.group li',data).attr("class"));
but this doesn't:
$('#content').prepend($('div #left ol.group li',data)[3].attr("class"));
Can someone explain to me why this doesn't work like most other functions do and how to make it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
jQuery 通过使用
eq
获取索引with jQuery get the index by using
eq
使用索引器访问 jQuery 集的单个元素会返回一个 DOM 对象,该对象没有
.attr()
方法。您需要.eq(3)
,它返回一个包装该单个元素的 jQuery 对象。Accessing an individual element of a jQuery set using the indexer returns a DOM object, which doesn't have the
.attr()
method. You want.eq(3)
, which returns a jQuery object wrapping that individual element.