首页 > 解决方案 > 如何获取函数调用的元素的事件?

问题描述

我正在尝试通过函数获取元素的事件,有没有机会做到这一点?基本上我只想拥有包含-“randomChosenColour”ID的元素的事件-

var gamePattern = []
var buttonCoulors = ["red", "blue", "green", "yellow", "wrong"]

$(document).on("keypress", nextSequence)

function nextSequence() {

  var randomNumber = Math.floor(Math.random() * 5)
  var randomChosenColour = buttonCoulors[randomNumber]
  var randomSound = "sounds/" + randomChosenColour + ".mp3"
  var Sound = new Audio(randomSound)
  gamePattern.push(randomChosenColour)

  if (randomNumber < 5) {
    $("#" + randomChosenColour).addClass("pressed")
    setTimeout(function() {
      $("#" + randomChosenColour).removeClass("pressed")
    }, 100)
  } else {}
  Sound.play()



}

标签: javascriptjqueryevents

解决方案


推荐阅读