使用javascript播放声音

play a sound using javascript

本文关键字:播放声音 javascript 使用      更新时间:2023-09-26

我正在尝试使用javascript 在事件上播放声音

试过这个

<object height="100" width="100" data="sounds/cash.wav" id="cash"></object>
var thissound= document.getElementById("cash");
thissound.play();

它奏效了。问题是,我不想在浏览器上显示我的元素,所以我尝试了

<object height="0" width="0" data="sounds/cash.wav" id="cash"></object>
and even
<object height="100" width="100" data="sounds/cash.wav" id="cash" style="display:none"></object>

我在JS中得到一个错误,告诉我元素为null。。

您可以尝试

<object height="100" width="100" data="sounds/cash.wav" id="cash" style="visibility: hidden; white-space: nowrap;"></object>

以隐藏它。

还可以将object元素包装在块中,并隐藏块。