开发者

How create a function method on FBJS that reference 'this' as HTML element?

have tried to use this my own function on FBJS:

function addEvent(event,fun,pars){
pars.event=event;
pars.listen=function(){ fun(this, pars);return false; };
this.addEventListener(event,pars.listen,false);
}

but dont work...if call:

obj=document.getElementById("id_element");
obj.addEvent(....);

Firebu开发者_StackOverflow中文版g return this error:am123456789_obj.addEvent is not a function

Any idea to resolve?


You didn't create the addEvent function as a property of the object, so you can't call obj.addEvent(). The object simply doesn't know about that function.

With FBJS, your best bet is to simply pass the object in as a parameter.

function addEvent(obj,event,fun,pars){
pars.event=event;
pars.listen=function(){ fun(obj, pars);return false; };
obj.addEventListener(event,pars.listen,false);
}

obj=document.getElementById("id_element");
addEvent(obj,...);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜