HTMLMediaElement:error 事件

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.

error 事件会在因为一些错误(如网络连接错误)导致无法加载资源的时候触发。

事件不可取消也不会冒泡。

语法

在类似 addEventListener() 这样的方法中使用事件名称,或设置事件处理器属性。

js
addEventListener("error", (event) => {});

onerror = (event) => {};

事件类型

一个通用的 Event

示例

js
const video = document.querySelector("video");
const videoSrc = "https://path/to/video.webm";

video.addEventListener("error", () => {
  console.error(`Error loading: ${videoSrc}`);
});

video.setAttribute("src", videoSrc);

规范

Specification
HTML Standard
# event-media-error
HTML Standard
# handler-window-onmessageerror

浏览器兼容性

BCD tables only load in the browser

参见