Mootools 类和绑定
我在访问类和当前元素时遇到问题。如何才能访问当前元素和类?
// 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一种更干净的方法。请注意,该事件已传递给该方法:
以及一个工作示例: http://jsfiddle.net/AtUgd/
Here's a cleaner way. Note that the event is already passed to the method:
And a working example: http://jsfiddle.net/AtUgd/
我通常会做这样的事情:
I usually do something like this:
请注意,
e.target
并不总是您真正期望的元素(您绑定的元素)。这是一个被单击的元素,因此如果您的绑定元素内部包含某些内容,则单击事件将冒泡 它会触发,但是
e.target
中会有错误的元素如果您想将事件绑定元素传递给您的类方法之一,您可以这样做:
现在您将正确的元素传递给您的方法并准备好进行操纵。
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:
Now you got the right element passed to your method and ready to manipulate on.