HTMLMediaElement:play 事件

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

paused 屬性從 true 變為 false 時,由 play 方法或 autoplay 屬性觸發 play 事件。

此事件不可取消,也不會冒泡。

語法

在諸如 addEventListener() 之類的方法中使用事件名稱,或設定事件處理程式屬性。

js
addEventListener("play", (event) => { })

onplay = (event) => { }

事件型別

一個通用的 Event

示例

這些示例為 HTMLMediaElement 的 play 事件添加了事件監聽器,然後在事件處理程式響應事件觸發後傳送一條訊息。

使用 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.",
  );
});

使用 onplay 事件處理程式屬性

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.",
  );
};

規範

規範
HTML
# event-media-play
HTML
# handler-onplay

瀏覽器相容性

另見