HTMLMediaElement: sự kiện play

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.

Sự kiện play được kích hoạt khi thuộc tính paused được thay đổi từ true thành false, do phương thức play hoặc thuộc tính autoplay.

Sự kiện này không thể hủy và không bong bóng.

Cú pháp

Sử dụng tên sự kiện trong các phương thức như addEventListener() hoặc đặt thuộc tính xử lý sự kiện.

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

onplay = (event) => { }

Kiểu sự kiện

Một Event chung chung.

Ví dụ

Các ví dụ này thêm trình xử lý sự kiện cho sự kiện play của HTMLMediaElement, sau đó đăng thông báo khi trình xử lý sự kiện đó phản ứng với việc kích hoạt sự kiện.

Sử dụng 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.",
  );
});

Sử dụng thuộc tính xử lý sự kiện 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.",
  );
};

Đặc tả kỹ thuật

Specification
HTML
# event-media-play
HTML
# handler-onplay

Tương thích trình duyệt

Sự kiện liên quan

Xem thêm