一直在尝试重新创建旧的掌上飞行员毒品 war 游戏,但无法弄清楚代码的哪一部分搞砸了。单击 + 按钮时尝试将值加 1。这是代码,谢谢!
init: function(){
this.acid = document.getElementById('acid');
this.acidQty = document.getElementById('acidQty');
this.acidAdd = document.getElementById('acidAdd');
},
// bind the events to the button
bindEvents: function(){
this.acidAdd.addEventListener('click', this.addAcid.bind(this), false);
// $("#acidAdd").on('click', this.addAcid.bind(this));
},
//Get the value
getQty: function(){
this.acidQuantity = $('#acidQty').val();
},
//get current value and add 1
addAcid: function(){
this.getQty();
this.acidQuantity ++;
this.updateQuantity();
},
// update the value on the html
updateQuantity: function(){
this.acidQty.innerText = this.acidQuantity;
},
这是所有代码的 jsbin,以防我错过其他内容
最佳答案
如果不查看所有代码,很难判断,但我相信以下更改应该有效。
this.acidQuantity: 1,
bindEvents: function(){
this.acidAdd.addEventListener.addEventListener('click', this.addAcid);
},
addAcid: function(){
this.updateQuantity();
this.acidQuantity ++;
},
updateQuantity: function() {
$('#acidQty').html(app.acidQuantity);
}
关于javascript - addeventlistener click 仅在第一次单击时起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39186835/