Javascript Mootools click event and his caller?
I have this little script:
var moolang = new Class({
initialize: function(element) {
this.el = $(element);
this.el.addEvent('click', this.popup); 开发者_开发技巧
},
popup: function()
{
//this.id = the id of the element.
}
});
And I want to know "this" in the popup function, but if I try something like alert(this.el.id)
it says there is no this.el.
Is there a way to know which class adds the event?
Change the attach event so the callee has the proper context. Otherwise the context of an event listener will be the target element.
// Change this line
this.el.addEvent('click', this.popup);
//To this
this.el.addEvent('click', this.popup.bind(this)); // popup->this == this
jsfiddle here See mootools documentation. Binding the context of a function.
精彩评论