我已经尝试了多种方式在一个按钮功能中添加音频到我的网页,但当按钮被点击时音频不播放。我的浏览器已经更新到最新版本,所以这不是问题。这是下面的代码,如果有人能告诉我哪里出了问题,那就太有帮助了。
Js
const rollSound = new Audio("sounds/short-guided-meditation.ogg");
document.getElementById('play').addEventListener("click", e => rollSound.play());
Html
<button class="playsound" id="play" onclick="rollSound.play()" >Play</button>
正在设置多个单击事件。它们可能会压倒彼此。rollSound
可以是html中的undefined
。一般来说,这些东西应该在控制台中显示错误。
const rollSound = new Audio("sounds/short-guided-meditation.ogg");
document.getElementById('play').addEventListener("click", e => rollSound.play());
从html
中删除点击处理程序<button class="playsound" id="play">Play</button>