javascript - 在动态生成的处理函数中访问 javascript 对象属性

标签 javascript jquery html

我有以下 Javascript 文件,其中一些 html 是动态生成的:

(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;
    this.generateHtml(el);
  },
  generateHtml: function(container){
        var bnext = document.createElement('input');
        bnext.setAttribute("type", "button");
        bnext.setAttribute("id", "bnext");
        bnext.onclick = this.nextImage();
        bnext.setAttribute("value", "Next");
        container.appendChild(bnext);

  },
  nextImage: function(){
     this.num++;


 }
 });

我想要做的是访问 nextImage 函数内的 NunoEstradaViwer 属性 num 但 this 对象指向 bnext 按钮。

你知道我该如何处理吗?

最佳答案

检查link

bnext.onclick = this.nextImage; // not bnext.onclick = this.nextImage();

另一件事是

NunoEstradaViwer.num++ ;  // instead of this.num++

关于javascript - 在动态生成的处理函数中访问 javascript 对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10656113/

相关文章:

javascript - anchor 标记 href 不是使用单斜杠导航而是使用双斜杠导航

javascript - 使用 JavaScript 将文本 chop 到特定大小 (8 KB)

jquery - 与蒙版混合的附加 alpha

javascript - 计算无限大父级中 HTML SPAN 的可能宽度?

html - Div Inside NavBar 100% 高度不工作

javascript - 根据key搜索并更新数组

php - 如何: Text field only accept certain words

jquery - 使用 jQuery 的简单 pubsub

html - 无法使用 Gmail API + rails 在 HTML 中显示嵌入式图像

html - 在移动设备上注释掉 HTML 代码