Mootools 类和绑定

发布于 2024-11-24 14:35:44 字数 475 浏览 3 评论 0原文

我在访问类和当前元素时遇到问题。如何才能访问当前元素和类?

// Class stuff above
fuction1 : function () {
  myelements.addEvent('click', this.function2);
},
function2 : function () {
  // "this" is the element from function1
  this.getParent('div')

  // now I need to call another function
  // But since the scope is the element, I get an error
  // If I bind function two,
  // how can I gain access to the element?
  this.function3();
}
//Class stuff below

谢谢!

I am having trouble accessing both the Class and the current element. How can I have access to both the current element and the Class?

// Class stuff above
fuction1 : function () {
  myelements.addEvent('click', this.function2);
},
function2 : function () {
  // "this" is the element from function1
  this.getParent('div')

  // now I need to call another function
  // But since the scope is the element, I get an error
  // If I bind function two,
  // how can I gain access to the element?
  this.function3();
}
//Class stuff below

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

萌逼全场 2024-12-01 14:35:45

这是一种更干净的方法。请注意,该事件已传递给该方法:

var Foo = new Class({

    initialize: function() {
        this.elements = $('li').addEvent('click', this.click.bind(this));
    },

    click: function(e) {
        console.log(this); // Foo Class
        console.log(e.target); // the clicked this.elements item
    }

});

以及一个工作示例: http://jsfiddle.net/AtUgd/

Here's a cleaner way. Note that the event is already passed to the method:

var Foo = new Class({

    initialize: function() {
        this.elements = $('li').addEvent('click', this.click.bind(this));
    },

    click: function(e) {
        console.log(this); // Foo Class
        console.log(e.target); // the clicked this.elements item
    }

});

And a working example: http://jsfiddle.net/AtUgd/

清欢 2024-12-01 14:35:45

我通常会做这样的事情:

FooBar = new Class({
    initialize: function() {
        this.element = $('foobar');
        this.element.addEvent('click', function(e) { this.foo(e) }.bind(this));
    },
    foo: function(e) {
        console.log( $(e.target) );
    }
});

I usually do something like this:

FooBar = new Class({
    initialize: function() {
        this.element = $('foobar');
        this.element.addEvent('click', function(e) { this.foo(e) }.bind(this));
    },
    foo: function(e) {
        console.log( $(e.target) );
    }
});
黑寡妇 2024-12-01 14:35:45

请注意,e.target 并不总是您真正期望的元素(您绑定的元素)。

这是一个被单击的元素,因此如果您的绑定元素内部包含某些内容,则单击事件将冒泡 它会触发,但是 e.target 中会有错误的元素

如果您想将事件绑定元素传递给您的类方法之一,您可以这样做:

var self = this; // 'this' is class reference
elements.addEvent('click', function(e){
    self.myClassMethod(this, e); // now 'this' is clicked element
});

现在您将正确的元素传递给您的方法并准备好进行操纵。

Note that e.target isn't always that element you really expect (the one you binded).

It's an element that is clicked so if your binded element contains something inside, click event will get bubbled up and it will fire, but you will have wrong element in e.target

If you want to pass event binded element to one of your class methods you can do it like this:

var self = this; // 'this' is class reference
elements.addEvent('click', function(e){
    self.myClassMethod(this, e); // now 'this' is clicked element
});

Now you got the right element passed to your method and ready to manipulate on.

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