我有一个关于在mootools课程中调用其他函数的问题。例如:
var f = new Class('foo',
{
test1: function(){
var table = new Element(...);
...
$(table).getElements('input').each(function(input) {
input.addEvent('change', function() {
// how could I call test2 and pass the input element?
})
});
},
test2: function(e){
alert(e);
}
});
谢谢。
答案 0 :(得分:1)
var f = new Class('foo',
{
test1: function(){
var table = new Element(........);
var me = this;
$(table).getElements('input').each(function(input) {
input.addEvent('change', function() {
me.test2("foo");
});
});
},
test2: function(e){
alert(e);
}
});
答案 1 :(得分:1)
var f = new Class('foo', {
test1: function() {
var table = new Element(........);
// no need to use $(), table is already an object.
table.getElements('input').addEvents({
change: function(e) {
this.test2(e);
}.bind(this) // bind the function to the scope of the class and
// not the element trigger
});
},
test2: function(e){
var e = new Event(e);
console.log(e.target);
}
});
答案 2 :(得分:0)
你必须使用bindWithEvent来获取你的函数中的事件和事件,你也不需要调用每个因素mootools会为你做这个:
var f = new Class('foo',
{
test1: function(){
var table = new Element(........);
table.getElements('input').addEvent('change', this.test2.bindWithEvent(this));
},
test2: function(e){
alert(e);
}
});