嘿,我发疯了就找不到解决方案
<div class="example">
<div>
<div></div>
</div>
<div>
<div></div>
</div>
<div> <-- Want to select this
<div></div> <-- but it always selects this
</div>
</div>
我想选择.example的最后一个div但是它总是选择.example的最后一个div中的最后一个div。如果我这样做.example:last-child ......怎么做对吗?
答案 0 :(得分:1)
尝试'div.example>div:last-child'
:
alert(document.getElementById('forCompare') === document.querySelector('div.example>div:last-child'));
<div class="example">
<div>
<div></div>
</div>
<div>
<div></div>
</div>
<div id="forCompare">
<div></div>
</div>
</div>