Javascript Mootools 点击事件和他的调用者?
我有这个小脚本:
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.
}
});
我想知道弹出函数中的“this”,但是如果我尝试类似 alert(this.el.id)
的内容,它会说没有 this.el。
有没有办法知道哪个类添加了事件?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改附加事件,以便被调用者拥有正确的上下文。否则,事件侦听器的上下文将是目标元素。
jsfiddle 在这里
请参阅 mootools 文档。绑定函数的上下文。
Change the attach event so the callee has the proper context. Otherwise the context of an event listener will be the target element.
jsfiddle here
See mootools documentation. Binding the context of a function.