Keydown事件无法正常工作

时间:2020-05-23 23:33:14

标签: javascript jquery function

我是javascript / jquery的新手,正在尝试运行以下代码:

function playSound(chosenColour) {

  var sound = new Audio("sounds/" + chosenColour + ".mp3");
  sound.play();
}

function nextInSequence() {
  var randomNumber = Math.floor(Math.random() * 4);
  var randomChosenColour = buttonColours[randomNumber];
  gamePattern.push(randomChosenColour);

  $("#" + randomChosenColour).animate({
    opacity: 0
  }, {
    duration: 50,
    start: function() {
      playSound(randomChosenColour);
    }
  }).animate({
    opacity: 1
  }, 50);
}

通过nextInSequence()事件侦听器调用keydown函数。由于某些原因,当我按下Alt,Ctrl,Shift或Windows键时,代码的animate部分会运行,但不会运行playSound()。有人可以解释为什么吗?

以下是相应的HTML:

<body>
  <h1 id="level-title">Press Any Key To Start</h1>
  <div class="container">
    <div class="row">

      <div type="button" id="green" class="btn green">

      </div>

      <div type="button" id="red" class="btn red">

      </div>
    </div>

    <div class="row">

      <div type="button" id="yellow" class="btn yellow">

      </div>
      <div type="button" id="blue" class="btn blue">

      </div>

    </div>

  </div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="game2.js" charset="utf-8"></script>
</body>

0 个答案:

没有答案