【问题标题】:Audio tag is not defined error in javascript [closed]javascript中未定义音频标签错误[关闭]
【发布时间】:2021-05-21 15:58:10
【问题描述】:
window.addEventListener("keydown",function(e){    // window is an object in the js which has all the global functions and variables as its methods and properties
    console.log(e);                                // window object represents the browser's window
    console.log(e.keyCode);                        // keycode is a function in keybooard event for representing the keycode of that particular key pressed
    const audio=document.querySelector(`audio[data-key="${e.keyCode}"]`)  // this catches the particular element of audio tag which has the data key of pressed button
    const key=document.querySelector(`.key[data-key="${e.keyCode}"]`)
    console.log(audio) 
    console.log(key) 
    if(!audio)    // if we press the key other than M,U,S,I,C
    {
        return;
    } 
    audio.currentTime=0;  // this attribute will help to play the audio from the time alloted on rhs after hitting the button

    audio.play()    // if we would not have put the current time then the sound will not come again as the previous sound is already been played 
    key.classList.add('playing')     // this is made to add playing class into the key class, when we press the button
})
    function removetransition(e)
    {
      console.log(e)
      if(e.propertyName!=="transform")   // we are taking only the transform propety from all the transition properties
      {
          return;
      }
      console.log(e.propertyName)       // this will be printed when the transition will be ended
      this.classList.remove("playing")
    }
    const keys=document.querySelectorAll(".key")
    keys.forEach(key =>key.addEventListener("transitionend",removetransition)); 
    // this function will give all the transition that are associated with the function in which the transitionend listner is added, and then by function definition we can remove it                                       
    // a bunch of things will be transitioned so it will give all the transition and then we can select which one we can remove
    window.addEventListener("keydown",function(e){
        if(e.keyCode==16)
        {
            audio.pause();
            key.classList.remove("playing")
        }

    })  

我正在用 Javascript 编写这个。当我运行e.keycode==16 事件侦听器时,它说未定义音频,即使我已经将音频定义为已经播放的声音。

【问题讨论】:

  • 请分享更多详细信息,例如您的代码的可运行示例和确切的错误消息。另外,请解释这与 CSS 有何关系,或删除该标签
  • @kmoser 你确定吗?至少有一个定义:const audio=document.querySelector(`audio[data-key="${e.keyCode}"]`)
  • 我刚刚删除了我的评论,因为我看到了。但我没有看到任何 HTML,所以很难说它是否真的选择了任何东西。
  • 为什么要添加两个 keydown 事件处理程序?第二个无法访问第一个处理程序中定义的audio 常量。
  • @AryanPandey 请edit your question 包含相关的 HTML。

标签: javascript html css web


【解决方案1】:

您应该全局声明audiokey

你的代码:

window.addEventListener("keydown", function (e) {
    console.log(e);
    console.log(e.keyCode);                        
    const key = document.querySelector(`.key[data-key="${e.keyCode}"]`)
    const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`)
    console.log(audio)
    console.log(key)
    if (!audio)
    {
        return;
    }
    audio.currentTime = 0;
    audio.play()
    key.classList.add('playing')
})

工作解决方案:

const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`)
const key = document.querySelector(`.key[data-key="${e.keyCode}"]`)

window.addEventListener("keydown", function (e) {
    console.log(e);
    console.log(e.keyCode);                        
    console.log(audio)
    console.log(key)
    if (!audio)
    {
        return;
    }
    audio.currentTime = 0;
    audio.play()
    key.classList.add('playing')
})

编辑:既然你重写了keydown 函数,那么你应该这样做:

window.addEventListener("keydown",function(e){
    console.log(e);
    console.log(e.keyCode);
    const audio=document.querySelector(`audio[data-key="${e.keyCode}"]`) 
    const key=document.querySelector(`.key[data-key="${e.keyCode}"]`)
    console.log(audio) 
    console.log(key) 
    if(!audio)
    {
        return;
    } 
    audio.currentTime=0;
    audio.play()
    key.classList.add('playing')
    if(e.keyCode==16)
    {
       audio.pause();
       key.classList.remove("playing")
    }
})
    function removetransition(e)
    {
      console.log(e)
      if(e.propertyName!=="transform")
      {
          return;
      }
      console.log(e.propertyName)
      this.classList.remove("playing")
    }
    const keys=document.querySelectorAll(".key")
    keys.forEach(key =>key.addEventListener("transitionend",removetransition)); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    • 2021-06-30
    • 2011-11-11
    • 1970-01-01
    • 2014-02-23
    相关资源
    最近更新 更多