Thuộc tính autoplay của đối tượng Video trong HTML DOM trả về hoặc thiết lập giá trị boolean cho biết video có tự động phát khi tải trang hay không.
Cú pháp
Trả về giá trị boolean – true/false:
mediaObject.autoplay
Thiết lập autoplay thành booleanValue:
mediaObject.autoplay = booleanValue
Ở đây, "giá trị boolean" có thể là:
| booleanValue | Chi tiết |
|---|---|
| true | Định nghĩa video sẽ tự động phát khi tải trang |
| false | Định nghĩa video sẽ không tự động phát khi tải trang |
Ví dụ
Ví dụ minh họa thuộc tính Video autoplay:
<!DOCTYPE html> <html> <head> <title>HTML DOM Video autoplay</title> <style> * { padding: 2px; margin:5px; } form { width:70%; margin: 0 auto; text-align: center; } input[type="button"] { border-radius: 10px; } </style> </head> <body> <form> <fieldset> <legend>HTML-DOM-Video-autoplay</legend> <video id="demo" width="320" controls><source src="https://www.tutorialspoint.com/html5/foo.mp4" type="video/mp4"></video><br> <input type="button" onclick="getTrackDetails()" value="Tắt tự động phát"> <div id="divDisplay"></div> </fieldset> </form> <script> var divDisplay = document.getElementById("divDisplay"); var demo = document.getElementById("demo"); demo.autoplay = true; divDisplay.textContent = 'Tự động phát: '+demo.autoplay; function getTrackDetails() { demo.autoplay = false; demo.load(); divDisplay.textContent = 'Tự động phát: '+demo.autoplay; } </script> </body> </html>
Kết quả
Trước khi nhấn nút „Tắt tự động phát“:

Sau khi nhấn nút „Tắt tự động phát“:
