使用 Mootools 和“这个”的困惑关键词
我正在尝试将事件处理程序分配给我在 Mootools 中创建的类,但我似乎无法访问我为该类创建的任何变量。就像这样:
var newPerson = new Class({
initialize: function(name)
{
this.firstName = name;
//-------Creating Div----------//
...........
//--------Created Div----------//
$(this.newDiv.id).click(function()
{
alert("clicked");
};
};
现在,当我更改函数来警告指定名称 alert(this.firstName);
的对象时,它不会访问它们,我不明白为什么。
有人能指出我正确的方向吗?
I am trying to assign an event handler to a class I have created in Mootools but I cannot seem to access any of the variables that I created for the class. Like so:
var newPerson = new Class({
initialize: function(name)
{
this.firstName = name;
//-------Creating Div----------//
...........
//--------Created Div----------//
$(this.newDiv.id).click(function()
{
alert("clicked");
};
};
Now when I change the function to alert the objects assigned name alert(this.firstName);
it doesn't access them and I can't figure out why.
Could anyone point my in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在“initialize”函数的顶部,添加一个变量声明:
现在,在您的处理程序中,您应该能够执行以下操作:
通过在“initialize”函数开始时保存
this
,您可以为“单击”处理程序提供一种获取原始对象的方法。在处理程序中,this
将引用与事件相关的 DOM 元素。 (我实际上是在猜测,因为我对 MooTools 不太熟悉。)At the top of the "initialize" function, add a variable declaration:
Now, in your handler, you should be able to do:
By stashing
this
as it stood when the "initialize" function began, you provide a way for the "click" handler to get at the original object. In the handler,this
will refer to the DOM element involved with the event. (I'm actually guessing about that, because I'm not really familiar with MooTools.)