【发布时间】:2023-02-11 22:36:08
【问题描述】:
我是 javascript 的新手。我正在尝试创建一个 notesapp,其中包含用户输入的笔记。到目前为止,我能够添加和显示注释,但每个注释都显示两次,例如,如果我输入“你好”,它会打印出你好,但如果我输入“再见”,它会打印出“你好,你好,再见。”我在下面打印了我的代码以及我的视图和模型类。
class NotesView{
constructor(model){
this.model = model;
this.mainContainerEL = document.querySelector('#main-container');
this.noteButton = document.querySelector('#add-note');
this.noteButton.addEventListener('click', () => { this.addNewNote() });
}
addNewNote(){
const new_note = document.getElementById('message').value;
this.model.addNote(new_note);
this.displayNotes();
}
displayNotes() {
const notes = this.model.getNotes();
notes.forEach(note =>{
const noteEl = document.createElement('div');
noteEl.textContent = note;
noteEl.className = 'note';
this.mainContainerEL.append(noteEl);
})
}
}
module.exports = NotesView;
class notesModel{
constructor(notes){
this.notes = []
}
getNotes(){
return this.notes
}
addNote(note){
return this.notes.push(note)
}
reset(){
this.notes.splice(0, this.notes.length)
}
}
module.exports = notesModel;
【问题讨论】:
标签: javascript class dom