2017-01-23 76 views
-1

我有一點問題,這段代碼在內部工作正常,但似乎在外部文件中removeTransition函數不見了。我錯過了什麼讓它工作?謝謝!函數在外部JavaScript文件中不起作用

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

你是如何'將'你的代碼附加到HTML? – Dellirium

+0

你能添加完整的html頁面代碼嗎? –

回答

0

您確定您正確引用.js文件嗎?

看看window.removeTransition()會做什麼。

你是如何正確調用你的函數?對不起,不能評論

+0

其他功能在外部文件中正常工作,只是這個.. – Raz

+0

對不起,我不明白你的問題。我剛開始學習JavaScript。 – Raz

+0

是你的e參數上的removeTransition(e)包含關鍵字this?嘗試改變this.classList.remove('playing');到e.classlist – Merigold

相關問題