我想选择网页上的最后一个 audio
元素 http://sycoscientistrecords.github.io/我尝试过:
$(" audio:last-of-type");
问题是它返回所有 audio
元素:
对象 { 长度:12,prevObject:对象,上下文:HTMLDocument→index.html,选择器:“audio:last-of-type”,还有 12 个…}
页面上有 12 个音频元素,jquery 选择器返回所有这些元素。
那么,如何使用 jquery 选择最后一个元素?
最佳答案
使用last()
。您也可以使用 :last
选择器,但它无法直接传递到浏览器的选择器引擎。
$("audio").last()
<小时/>
Why doesn't
last-of-type
work?
jQuery docs对于选择器来说:
Selects all elements that are the last among siblings of the same element name.
因此,在您的情况下,它将匹配其他元素内的所有最后一个 audio
元素。
关于javascript - 如何用jquery选择最后一个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37539383/