我想在 jquery 代码块中使用调用对象的值,但是“this”映射到 jquery 对象而不是调用者!请问如何解决这个问题?
// class
myClass = function (){
// member object
this._localVars = {
_elementClass:'.elem-class',
_dots:null,
_dotStatus:null
};
// member function
this.func1 = function() {
$(this._elementClass).each(function(_index, _element){
// this._localVars._dots[_index] = _element; ... this line throws an error 'this._localVars' is undefined ... as 'this' is html element here and not an object of the calling class
});
};
};
请建议我如何在 jquery 代码块中使用“this”来引用类的变量/对象而不是 HTML/jQuery。
谢谢
最佳答案
尝试将this
对象保存到局部变量中
var myObject = this;
$(this._elementClass).each(function(_index, _element){
myObject._localVars._dots[_index] = _element;
});
关于javascript - 在 jquery 代码块中使用时出现 'this' 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3953619/