开发者

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜