我正在创建一个 Javascript 对象,其中包含一个执行 jQuery each
方法的函数,如下所示:
function MyClass {
Method1 = function(obj) {
// Does something here
}
Method2 = function() {
$(".SomeClass").each(function() {
// 1 2
this.Method1(this);
});
}
}
每个 THIS
指的是哪个对象? jQuery 指的是从 each
迭代返回的项目。但是,我希望 This[1]
引用包含的类...
如何在 jQuery 循环中引用包含类?
最佳答案
我想你可以做这样的事情:
function MyClass {
Method1 = function(obj) {
//do something here
}
Method2 = function () {
var containingClass = this;
$(".SomeClass").each(function () {
containingClass.Method1(this);
});
}
}
}
关于javascript - Jquery $().each方法掩盖了 'this'关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/980821/