0

我的问题是这个。当我在下面的audioBoing 函数中添加一个参数,然后将相同的参数放在getElementById字符串中时,该函数不起作用。我收到一个错误,显示未捕获的类型错误,无法调用 null 的方法“AddEventListener”

下面的功能工作正常。我重写了它下面的函数以反映我正在尝试做的事情。最终,我试图抽象出函数的很大一部分,这样我就可以插入参数并运行它,而不必每次为它存储/启动的每个声音都重写它。

var playAudioFileOneDrumOneBig = function () {
var source = context.createBufferSource();
source.buffer = savedBufferOne;
source.connect(delay.input);
delay.connect(convolver.input);
convolver.connect(context.destination);
source.noteOn(0); // Play sound immediately
};




function audioBoing()  
var xhr = new XMLHttpRequest();
xhr.open('get', 'audio/F.mp3', true);
xhr.responseType = 'arraybuffer';
xhr.onload = function () {
        context.decodeAudioData(xhr.response,
             function(incomingBuffer1) {
                 savedBufferOne = incomingBuffer1;
                 var noteOneDrumOneBig = document.getElementById("noteOneDrumOneBig"); 
                 noteOneDrumOneBig.addEventListener("click", playAudioFileOneDrumOneBig , false);
             }
        );
};
xhr.send();
};

audioBoing();

重写不工作

function audioBoing(yay) {      //added yay

this.yay=yay;                 // defined yay

var xhr = new XMLHttpRequest(); 
xhr.open('get', 'audio/F.mp3', true);
xhr.responseType = 'arraybuffer';
xhr.onload = function () {
        context.decodeAudioData(xhr.response,
             function(incomingBuffer1) {                  
                 savedBufferOne = incomingBuffer1;    
                 var noteOneDrumOneBig = document.getElementById(yay);           //passed yay
                 noteOneDrumOneBig.addEventListener("click", playAudioFileOneDrumOneBig , false);   //error happens here
             }
        );
};
xhr.send();
};

audioBoing(noteOneDrumOneBig);
4

1 回答 1

2

您没有引用传递给的字符串audioBoing

audioBoing("noteOneDrumOneBig");
于 2012-11-26T07:17:58.807 回答