这是我的第一篇文章,我已经搜索了一些答案,但没有找到任何解决方案。
基本上我想做的就是使用 JavaScript 函数 playSound(sound) 让 onClick=""启动和停止音频。到目前为止我已经结束了。现在,当我单击时没有音频播放,但是当我单独测试单个代码“song1.play()”时,声音会播放,但再次单击时显然不会停止。希望这不会太困难。
function playSound(sound){
var song1=document.getElementById(sound);
var isPlaying = true;
if (!isPlaying){
isPlaying == true;
song1.play();
}
else{
isPlaying == false;
song1.pause();
}
}
最佳答案
两个小修正。
a) var isPlaying = true;
应全局声明,以在多次调用“OnClick”之间保留其值。
b) `isPlaying' 变量的赋值语句中的 ==
应更改为 =
。
var isPlaying = true;
function playSound(sound){
var song1=document.getElementById(sound);
if (!isPlaying){
isPlaying = true;
song1.play();
}
else{
isPlaying = false;
song1.pause();
}
}
关于javascript - 使用 javascript onClick 启动/停止声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46497870/