我正在尝试获取 <div>
的最后一个 child 但我失败了。这是我使用的代码,但它总是返回 undefined
.
$('#output:last-child').attr('class');
出了什么问题?我该如何修复它?
这应该是最后一个元素:
<div class='hisout ${hisclass}'><p class="he">` + message + '</p></div>
最佳答案
使用子组合器选择器:
$('#output > :last-child').attr('class');
编辑:添加示例
#output > :last-child {
height: 20px;
width: 20px;
background: red;
border: 1px solid green;
}
<div id="output">
<div>....</div>
<div>....</div>
<div>....</div>
</div>
关于javascript - 无法获取 div 的最后一个子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56345996/