javascript - 如何用jquery选择最后一个元素?

标签 javascript jquery html audio

我想选择网页上的最后一个 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/

相关文章:

javascript - 如何使用jQuery停止页面中的所有ajax进程?

html - 如何使禁用的按钮变灰

javascript - lessc 使用 Ruby 而不是 Javascript

javascript - 如何将 "this"与一堆事件监听器一起使用?

javascript - Angular 4 DOM 操作和搜索

javascript - 将 jquery panzoom 与 jquery draggable 一起使用

html - 我怎样才能让这个CSS自动适合屏幕

javascript - 将 HTML 子元素移出父元素并删除父元素

javascript - 如何为用户创建多个相同表单

javascript - 如何从 Botkit hears 方法中接受多个值?