gpt4 book ai didi

javascript - 无法使第二个事件监听器适用于 MVC 计数器应用程序

转载 作者:行者123 更新时间:2023-11-28 05:27:19 27 4
gpt4 key购买 nike

我正在尝试使用 MVC 方法创建一个简单的计数器应用程序,但我无法弄清楚为什么向下计数器的事件监听器不起作用,但向上计数器却起作用。肯定有一些我忽略的东西,但我对这种方法完全陌生。任何帮助将不胜感激。

//model 
var model =
{
clickCount: 0
};


//controller

var controller = {

init: function() {
view.init();
},
incrementCounterUp: function() {
counter.textContent = model.clickCount += 1;
},
incrementCounterDown: function() {
counter.textContent = model.clickCount -= 1;
}
};


//view
var counter = document.createElement('p');
counter.textContent = (model.clickCount);
counter.id = counter;
document.body.appendChild(counter);

var upButton = document.createElement("button");
upButton.id = upButton;
document.body.appendChild(upButton);

var upButtonText = document.createTextNode("Up");
upButtonText.id = upButtonText;
upButton.appendChild(upButtonText);

var downButton = document.createElement("button");
downButton.id = downButton;
document.body.appendChild(downButton);

var downButtonText = document.createTextNode("Down");
downButtonText.id = downButtonText;
downButton.appendChild(downButtonText);



var view = {
init: function() {
// store pointers to our DOM elements for easy access
this.counterElem = document.getElementById(counter);
this.upButtonElem = document.getElementById(upButton);
this.upButtonElem.addEventListener('click', function(){
controller.incrementCounterUp();
});

this.downButtonElem = document.getElementById(downButton);
this.downButtonElem.addEventListener('click', function(){
controller.incrementCounterDown();
});
}
};


controller.init();

最佳答案

contoller.incrementCounterDown();

您拼错了“ Controller ”。 :P

关于javascript - 无法使第二个事件监听器适用于 MVC 计数器应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40033382/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com