我正在尝试使用 jquery 对 div 进行扩展。 该扩展称为 NunoEstradaViewer,下面是代码示例:
(function ($){
NunoEstradaViwer: {
settings: {
total: 0,
format: "",
num: 0;
},
init: function (el, options) {
if (!el.length) { return false; }
this.options = $.extend({}, this.settings, options);
this.itemIndex =0;
this.container = el;
this.total = this.options.total;
this.format = ".svg";
this.num = 0;
},
generateHtml: function(){
/*GENERATE SOME HTML*/
$("#container").scroll(function(){
this.num++;
this.nextImage;
})
},
nextImage: function(){
/*DO SOMETHING*/
}
});
我的问题是我需要访问 this.num 的值并在滚动事件的处理函数中调用函数 this.nextImage,但是对象“this”指的是滚动而不是“NunoEstradaViewer” .我怎样才能访问这些元素?
谢谢
最佳答案
通常我在这种情况下所做的是将对“this”的引用保存在一个变量中。
generateHtml: function(){
/*GENERATE SOME HTML*/
var self = this;
$("#container").scroll(function(){
self.num++;
self.nextImage;
})
}
关于javascript - JQuery - 在事件处理程序中访问对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10654206/