以下内容有什么不同:
$('.mydiv').children().last() 和
$('.mydiv').last()?例如,当我们想要获取div中的最后一个孩子时,哪种方法更好?
发布于 2016-04-19 15:47:34
使用这个
$('.mydiv').children().last()因为$('.mydiv').last()不会提供任何子级
发布于 2016-04-19 15:48:03
就像听起来的那样。
$('.mydiv').children().last()选择.mydiv元素的子元素,然后从该子元素列表中获取最后一项。
$('.mydiv').last()选择所有的.mydiv元素,然后获取列表中的最后一个元素。
如果您有多个$('.mydiv')元素,那么$('.mydiv').children().last()将获取最后一个$('.mydiv')的最后一个子元素。如果您需要获取特定$('.mydiv') div的最后一个子项,那么可以使用first()、last()或eq(index)来获取特定项,然后获取最后一个子项。
//this select the second .myDiv then get its children and then the last of those children
$('.mydiv').eq(1).children().last(); 发布于 2016-04-19 15:49:24
在$('.mydiv').children().last()中引用的是.mydiv的最后一个子元素,在$('.mydiv').last()中引用的是$('.mydiv')元素集合的最后一个元素
https://stackoverflow.com/questions/36711762
复制相似问题