Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JQuery-事件处理程序内的Access对象属性_Javascript_Jquery - Fatal编程技术网

Javascript JQuery-事件处理程序内的Access对象属性

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

我正在尝试使用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的值,并在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++}