我正在做一個小項目來記筆記。每次我單擊“添加新注釋”時,都會添加注釋。單擊第二次或多次“添加”按鈕后,循環會不斷添加錯誤數量的注釋。首先是 1,然后是 3、6、10,依此類推。document.querySelector('#newNoteBtn').addEventListener('click', onNewNote);function onNewNote() { const title = document.querySelector('#noteTitle').value;const content = document.querySelector('#noteContent').value;const note = { title: title, content: content, colour: '#ff1455', pinned: false, createDate: new Date()}notes.push(note);console.log(note);localStorage.setItem(lsNotesKey, JSON.stringify(notes));const notesFromLocalStorage = JSON.parse(localStorage.getItem(lsNotesKey));const convertedNotes = notesFromLocalStorage.map( note => { note.createDate = new Date(note.createDate); return note;});const notesContainer = document.querySelector('main');for (const note of convertedNotes) { const htmlNote = document.createElement('section'); const htmlTitle = document.createElement('h1'); const htmlContent = document.createElement('p'); const htmlTime = document.createElement('time'); const htmlButton = document.createElement('button'); htmlNote.classList.add('note'); htmlTitle.innerHTML = note.title; htmlContent.innerHTML = note.content; htmlTime.innerHTML = note.createDate.toLocaleString(); htmlButton.innerHTML = 'remove'; htmlButton.addEventListener('click', removeNote); htmlNote.appendChild(htmlTitle); htmlNote.appendChild(htmlContent); htmlNote.appendChild(htmlTime); htmlNote.appendChild(htmlButton); notesContainer.appendChild(htmlNote);}}
循環使筆記應用程序中的筆記數量加倍
不負相思意
2023-07-06 19:56:37