jQuery div 中具有类名的最后一个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2123228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:48:59 来源:igfitidea点击:
last element with a class name in a div
提问by Luca Romagnoli
how can i get the last div with class a in a div the id = test ? in this case i have to get the div with content = 1000
我怎样才能在一个 id = test 的 div 中获得最后一个类 a 的 div?在这种情况下,我必须获得内容 = 1000 的 div
<div id="test">
<div class="a">1</div>
..
..
<div class="a>1000</div>
</div>
回答by Tom
回答by Joel A. Villarreal Bertoldi
Without jQuery:
没有 jQuery:
var divs = document.getElementById("test").getElementsByTagName("div");
var lastChild = divs[divs.length - 1];
回答by Reigel
$('div#test div:last-child');