Javascript JQuery-事件处理程序内的Access对象属性
我正在尝试使用jquery对div进行扩展。 扩展名为NunoEstradaViewer,下面是代码示例:Javascript JQuery-事件处理程序内的Access对象属性,javascript,jquery,Javascript,Jquery,我正在尝试使用jquery对div进行扩展。 扩展名为NunoEstradaViewer,下面是代码示例: (function ($){ NunoEstradaViwer: { settings: { total: 0, format: "", num: 0; }, init: function (el, options) { if (!el.length) { return false; } this.options = $.e
(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的值,并在scroll事件的处理函数中调用函数this.nextImage,但对象“this”指的是滚动,而不是“NunoEstradaViewer”。如何访问这些元素
谢谢在这种情况下,我通常会在变量中保存对“this”的引用
generateHtml: function(){
/*GENERATE SOME HTML*/
var self = this;
$("#container").scroll(function(){
self.num++;
self.nextImage;
})
}
常见的解决方案是存储对所需上下文的引用:
(function () {
var self;
self = this;
$('#container').scroll(function () {
self.doStuff();
});
}());
另一种方法是将上下文传递给函数:
(function () {
$('#container').scroll({context: this, ..more data to pass..}, function (e) {
e.data.context.doStuff();
});
//alternatively, if you're not passing any additional data:
$('#container').scroll(this, function (e) {
e.data.doStuff();
});
}());
谢谢你的回答。我设法这样做:$('#container').bind('scroll',{viewer:this},function(event){event.data.viewer.num++}