Phi*_*ess 0 javascript mootools javascript-events javascript-framework
我有这个小脚本:
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.
}
});
Run Code Online (Sandbox Code Playgroud)
我想在弹出功能中知道"this".但如果我尝试像alert(this.el.id)这样的东西,它就说没有这个.el:/
有没有办法知道哪个类添加了事件?
更改附加事件,以便被调用者具有适当的上下文.否则,事件侦听器的上下文将是目标元素.
// Change this line
this.el.addEvent('click', this.popup);
//To this
this.el.addEvent('click', this.popup.bind(this)); // popup->this == this
Run Code Online (Sandbox Code Playgroud)
jsfiddle在这里 查看mootools 文档.绑定函数的上下文.