我有以下 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/