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

View in English Always switch to English

HTMLMediaElement: play event

基线 广泛可用

自 2015年7月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

paused 属性由 true 转换为 false 时触发 play 事件,事件触发原因一般为 play() 方法调用,或者 autoplay 标签设置。

Bubbles No
Cancelable No
Interface Event
Target Element
Default Action None
Event handler property GlobalEventHandlers.onplay
Specification HTML5 media

Examples

下方的例子监听了 HTMLMediaElement 标签的 play 事件,并且在事件触发后在控制台打印相应的信息。

Using addEventListener():

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

video.addEventListener("play", (event) => {
  console.log(
    "The Boolean paused property is now false. Either the " +
      "play() method was called or the autoplay attribute was toggled.",
  );
});

Using the onplay event handler property:

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

video.onplay = (event) => {
  console.log(
    "The Boolean paused property is now false. Either the " +
      "play() method was called or the autoplay attribute was toggled.",
  );
};

Specifications

规范
HTML
# event-media-play
HTML
# handler-onplay

Browser compatibility

See Also