使用 Mootools 和“这个”的困惑关键词

发布于 2024-11-03 11:59:51 字数 464 浏览 10 评论 0原文

我正在尝试将事件处理程序分配给我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧话新听 2024-11-10 11:59:51

在“initialize”函数的顶部,添加一个变量声明:

var thisObj = this;

现在,在您的处理程序中,您应该能够执行以下操作:

alert(thisObj.firstName);

通过在“initialize”函数开始时保存 this,您可以为“单击”处理程序提供一种获取原始对象的方法。在处理程序中,this 将引用与事件相关的 DOM 元素。 (我实际上是在猜测,因为我对 MooTools 不太熟悉。)

At the top of the "initialize" function, add a variable declaration:

var thisObj = this;

Now, in your handler, you should be able to do:

alert(thisObj.firstName);

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文