HTMLMediaElement: ended 事件

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

We’d love to hear your thoughts on the next set of proposals for the JavaScript language. You can find a description of the proposals here.
Please take two minutes to fill out our short survey.

ended 事件会在媒体回放或者媒体流因达到了媒体的未尾或者没有更多可用的数据而停止时被触发。

该事件会在回放或媒体文件播放结束时,在媒体元素<audio><video>)上触发。

是否冒泡
是否可取消
接口 Event
目标 元素
默认行为
事件处理器属性 GlobalEventHandlers.onended

备注: 该事件也在媒体捕捉与媒体流Web Audio 这两个 API 中定义。

示例

以下示例展示了如何为媒体元素的 ended 事件添加一个事件监听器,以在该事件触发时发送一条消息。

使用 addEventListener()

js
const video = document.querySelector("video");

video.addEventListener("ended", (event) => {
  console.log(
    "Video stopped either because 1) it was over, " +
      "or 2) no further data is available.",
  );
});

使用 onended 事件处理器属性:

js
const video = document.querySelector("video");

video.onended = (event) => {
  console.log(
    "Video stopped either because 1) it was over, " +
      "or 2) no further data is available.",
  );
};

规范

Specification
HTML
# event-media-ended
HTML
# handler-onended

浏览器兼容性

相关事件

参见