我有以下代码将事件监听器附加到表单提交。我要调用的功能是回调'。 事情就是这样:我不明白我应该如何将参数传递给函数(它应该是'事件'参数?)
if(form.addEventListener){
form.addEventListener('submit', interrupt, false);
}else if(form.attachEvent){
form.attachEvent('onsubmit', interrupt);
}
function interrupt(event){
//deal with form submission here
}
//OR (???)
interrupt = function(event){
//deal with form submission here
}
我知道这个问题可能看起来很基本,但我真的不明白它应该如何运作......
答案 0 :(得分:0)
Run this example and look for console of browser. When bind dynamic event, the event is injected. For this, the function call must receive the first parameter
var input = document.getElementById('ipTest');
if(input.addEventListener){
input.addEventListener('click', interrupt, false);
}else if(input.attachEvent){
input.attachEvent('onclick', interrupt);
}
function interrupt(event){
console.log("Event: " + event);
}
<input type="button" value="Test" id="ipTest"/>