简体   繁体   English

Javascript switch, case, break 不工作

[英]Javascript switch, case, break doesn't work

I'm new at JS我是 JS 的新手

I'm making a drum kit website and i create a switch section but it's not working.我正在制作一个架子鼓网站并创建了一个开关部分,但它不起作用。

var numberOfDrumButtons = document.querySelectorAll(".drum").length;

for (var i = 0; i < numberOfDrumButtons; i++) {

  document.querySelectorAll(".drum")[i].addEventListener("click", function() {


    var buttonInnerHtml = this.innerHTML;

    switch (buttonInnerHtml) {
      case "w":
        var tom1 = new Audio('songs/oni-chan.mp3');
        tom1.play();
        break;

      case "a":
        var tom2 = new Audio('songs/oni-chan2.mp3');
        tom2.play();
        break;

      case "s":
        var tom3 = new Audio('songs/oni-chan3.mp3');
        tom3.play();
        break;
      default:
        break;
    }

  })
}    
    

HTML: HTML:

  <div class="set">
    <button class="w drum"><span>w</span></button>
    <button class="a drum"><span>a</span></button>
    <button class="s drum"><span>s</span></button>
    <button class="d drum"><span>d</span></button>
    <button class="j drum"><span>j</span></button>
    <button class="k drum"><span>k</span></button>
    <button class="l drum"><span>L</span></button>
  </div>

  <script src="index.js" href="UTF-8"></script>

From the Audio docs( https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement/Audio#determining_when_playback_can_begin ):来自音频文档( https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement/Audio#determining_when_playback_can_begin ):

There are three ways you can tell when enough of the audio file has loaded to allow playback to begin:您可以通过三种方式判断何时加载了足够的音频文件以允许开始播放:

  • Check the value of the readyState property.检查 readyState 属性的值。 If it's HTMLMediaElement.HAVE_FUTURE_DATA, there's enough data available to begin playback and play for at least a short time.如果它是 HTMLMediaElement.HAVE_FUTURE_DATA,则有足够的数据可用于开始播放并播放至少一小段时间。 If it's HTMLMediaElement.HAVE_ENOUGH_DATA, then there's enough data available that, given the current download rate, you should be able to play the audio through to the end without interruption.如果它是 HTMLMediaElement.HAVE_ENOUGH_DATA,那么有足够的可用数据,根据当前的下载速率,您应该能够不间断地播放音频直到结束。

  • Listen for the canplay event.收听 canplay 事件。 It is sent to the element when there's enough audio available to begin playback, although interruptions may occur.当有足够的音频可以开始播放时,它会被发送到元素,尽管可能会出现中断。

  • Listen for the canplaythrough event.监听 canplaythrough 事件。 It is sent when it's estimated that the audio should be able to play to the end without interruption.当估计音频应该能够不间断地播放到最后时发送。 The event-based approach is best:基于事件的方法是最好的:

myAudioElement.addEventListener("canplaythrough", event => {
  /* the audio is now playable; play it if permissions allow */
  myAudioElement.play();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM