Javascript 如何在单击时暂停音乐?

Javascript 如何在单击时暂停音乐?,javascript,html,jquery,css,Javascript,Html,Jquery,Css,我用下面的代码(我在网上找到的)在我的网站上点击按钮时播放音频,现在我很好奇,当用类似的代码点击同一按钮时,我能做些什么使音频暂停和/或停止播放 const rollSound = new Audio("./mp3/SoItGoes.mp3"); $('#Triangle').click(e => rollSound.play()); 您可以在按钮上使用一个类来指定播放机的状态(class=“playing”如果正在播放,则为nothing,如果暂停,则为nothing),并在单击按钮时

我用下面的代码(我在网上找到的)在我的网站上点击按钮时播放音频,现在我很好奇,当用类似的代码点击同一按钮时,我能做些什么使音频暂停和/或停止播放

const rollSound = new Audio("./mp3/SoItGoes.mp3");
$('#Triangle').click(e => rollSound.play());

您可以在按钮上使用一个类来指定播放机的状态(
class=“playing”
如果正在播放,则为nothing,如果暂停,则为nothing),并在单击按钮时进行检查:

HTML:


您可以在事件处理程序中使用if语句来检查按钮文本值,您可以使用如下内容:

HTML

<button id="Triangle">Play</button>
const rollSound = new Audio("./mp3/SoItGoes.mp3");
$('#Triangle').click((e) => {
  if($('#Triangle').text() === 'Play') {
      rollSound.play();
      $('#Triangle').text('Pause')
  }
  else {
    rollSound.pause();
    $('#Triangle').text('Play')
  }
});
这对你有用吗<代码>滚动声音。暂停是停止声音的方式。
<button id="Triangle">Play</button>
const rollSound = new Audio("./mp3/SoItGoes.mp3");
$('#Triangle').click((e) => {
  if($('#Triangle').text() === 'Play') {
      rollSound.play();
      $('#Triangle').text('Pause')
  }
  else {
    rollSound.pause();
    $('#Triangle').text('Play')
  }
});