例如我在这里有一个for循环,它将在div内生成一些数字
var countProfile = 0;
var tablehtml = "";
for (var i=0; i <10; i++){
tablehtml = tablehtml + '<div class="DataCount">'+countProfile+'</div>'
countProfile++;
};
$("#somewhere").html(tablehtml);
我试图抓住最后一个孩子使用另一种方法,我试图使用:last-child()
它,但是在我的应用程序中不起作用,我做错了吗?
var dataCounting = $(".DataCount:last-child").html();
console.log(dataCounting);
:last-child
选择他们父母中的最后一个,如果他们在另一个父母中,则可以选择多个,而不是尝试last()
或:last
$(".DataCount").last().html();
或者,如果只有一个父元素并且包含不同类型的元素,则有时:last-child
可能无法按预期工作(有关更多信息::nth-child和:nth-of-type的区别),因此在这种情况下,请使用:last-of-type
。
$(".DataCount:last-of-type").html();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句