简体   繁体   English

如何找到元素的最后一个子元素?

[英]How to find the last child of an element?

 <div id="main">
  <div class="1"></div>
  <div class="2"></div>
  <div class="2"></div>
  <div class="3"></div>
  <div class="3"></div>
  <div class="4"></div>
 </div>

How we can write a selector using :last-child in jquery to find out the last div with class 3 ? 我们如何编写一个选择器使用:last-child jquery中的:last-child来找出last div with class 3last div with class 3

Try this selector... though your classes shouldn't be just numbers. 试试这个选择器......虽然你的课不应该只是数字。 They should start with a normal character. 他们应该以正常的角色开始。

$("#main .3:last")

FYI, :last-child checks if the target is last child of its parent. 仅供参考, :last-child检查目标是否是其父项的最后一个子项。 None of the elements with class .3 are the last child of their parent. .3的元素都不是其父类的最后一个子元素。

$('#main > .3').last()应该指向正确的元素

尝试这个:

$('#main').find(':last-child');

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

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