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 = $.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/

相关文章:

javascript - 为什么每次单击按钮时我的表格都会重复?

jquery - 如何在 Perl 中访问通过 Ajax jquery 传递的数组

javascript - iframe 中的多个颜色框

javascript - 从 S3 动态加载模块到 node.js AWS lambda

javascript - 用连字符分隔匹配值

javascript - Typescript - 找不到名称 'fetch'(通用库)

javascript - 在 jQuery 中使用 Each() 和 Children() 形成计算器

javascript - AngularJS 动画回调,等待下一个动画开始

javascript - 为什么动态添加的标签( Angular Directive(指令))给出未定义?

jquery - Ember.js 1.2.0 debounce 在 jquery 事件上无法按预期工作