简体   繁体   English

我如何在按班级检索的项目数组中获得第n个孩子?

[英]How can I get nth child in array of items retrieved by class?

I have a few <a> elements with the class brandPagination 我的brandPagination有几个<a>元素

If I use this: 如果我使用这个:

console.log($j('.brandPagination'));

I get an array with five items in it. 我得到一个包含五个项目的数组。 I want the nth one. 我要第n个。 So I tried: 所以我尝试了:

console.log($j('.brandPagination:nth-of-type(1)'));

This outputs an empty array, why and what can I do about it? 这将输出一个空数组,为什么以及我该怎么办?

You can either use 您可以使用

$j('.brandPagination:eq(0)');
// or 
$j('.brandPagination').eq(0);

Keep in mind nth-of-type starts in 1, and jQuery's eq is zero based. 请记住, nth-of-type从1开始,而jQuery的eq从零开始。

Use the last-child selector to fetch the last item of the selector specified before. 使用last-child选择器来获取之前指定的选择器的最后一项。

Try this out, 试试看

console.log($j('.brandPagination:last-child'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM