带参数的事件绑定?兼容ie和firefox~高手进
function addEvent(elm, evType, fn, useCapture){
if (elm.addEventListener){
elm.addEventListener(evType, fn, useCapture);
return true;
} else if (elm.attachEvent){
var r = elm.attachEvent("on"+evType, fn);
return r;
} else {
alert("Handler could not be removed");
}
}
以上函数可以进行事件绑定,而且兼容ie和firefox,但是不能传递参数,不知如何改?
[解决办法]
楼主的代码是没有参数啊
在事件处理函数中带参数,可以使用闭包,参考代码:
//事件处理函数//在事件处理function createHandler(param1, param2){ return function Add() { return param1 + param2; }}<input type="button" onclick="createHandler(1, 2)" />
[解决办法]
<input onclick="alert()" type=button value="test"><input type=button value="click me" onclick="this.previousSibling.onclick=function (event){testFun(event,this,'12')};this.disabled=true;">
<script>
function testFun(event,obj,str)
{
event=event||window.event
alert(event);
alert(obj.onclick);
alert(str);
}
</script>
[解决办法]
你查查call ,apply 的用法,就是利用函数名,函数的参数 ,组合成新的带参数的函数
[解决办法]
<script>owner = {id:'a',name:'b'}function handler(myparam){ alert(this.id+' '+this.name+' '+myparam)}handler.call(owner, 'c');//will alert 'a b c';</script>