Possible Duplicate:
How to get nth jQuery element
Get an element by index in jquery
用于从数组中选择 Nth 项的方括号 [ ] 索引符号的 jQuery 等效项是什么?
假设您有 10 个段落,想要获取第 7 个段落的内容。使用方括号表示法可行,但如果您想继续使用 jQuery,则不行:
$("p")[6]; //returns DOM object: [object HTMLParagraphElement]
$("p")[6].html(); //returns error: (Chrome:) Uncaught TypeError: Object #<HTMLParagraphElement> has no method 'html' (Firefox:) TypeError: $("p")[6].html is not a function (IE8+:) TypeError: Object doesn't support this property or method (IE7:) [object Error]
最佳答案
方括号 [n] 索引表示法有效,但您得到的是 native Javascript DOM 对象,而不是 jQuery 可以使用的对象。使用 jQuery 的 .eq()
method或:eq()
selector获取 jQuery 对象。
$("p").eq(6).html();
$("p:eq(6)").html();
( jsfiddle example )
关于javascript - 从 jQuery 数组或元素对象列表中选择第 N 个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11638392/