1. 面向开发者的 Web 技术
  2. Web API
  3. HTMLMediaElement
  4. HTMLMediaElement: ended 事件

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

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 ⁨2015年7月⁩.

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

浏览器兼容性

相关事件

参见

Help improve MDN

Learn how to contribute

This page was last modified on by MDN contributors.

AltStyle によって変換されたページ (->オリジナル) /