我建立了吃豆人游戏,我想在游戏输掉的那一刻打开播放视频内容的模式,而无需点击按钮或进行任何其他操作。
我尝试过:
test.html:
<!DOCTYPE html>
<html>
<head>
<script src="./jquery-3.4.1.slim.min.js"></script>
<script src="./test.js"></script>
<link rel="stylesheet" type="text/css" href="teststyle.css">
</head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<body>
<button class="trigger">Click here to trigger the modal!</button>
<div class="modal">
<div class="modal-content">
<div>
<video id="video1" width="400" muted="muted" allow="autoplay">
<source src="./assets/video/coffindance.mp4" allow="autoplay" type="video/mp4">
Your browser does not support HTML5 video.
</div>
<button class="Restart">Restart</button>
</div>
</div>
</body>
</html>
test.js:
$(document).ready(function() {
var modal = document.querySelector(".modal");
modal.classList.toggle("show-modal");
$('#video1')[0].muted=false;
$('#video1')[0].play();
})
我收到此错误:
test.js:8 Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first.
我该如何解决?