默認文本應該顯示在帶有 id 'notes' 的 div 中。包含用于顯示注釋的按鈕的 Javascript 代碼。有條件顯示更新的注釋和默認文本。給出了在更新之前應該顯示的默認文本部分。但是,不顯示默認文本。我沒有發(fā)現(xiàn)任何錯誤。怎么做?<hr><h1>Your Notes:</h1><hr><!-- container-fluid use up all the available spaces --><div id="notes" class="row container-fluid"></div>function show_notes() { let notes = localStorage.getItem("notes"); if (notes == null) { notesObj = []; } else { notesObj = JSON.parse(notes); } let html = ""; notesObj.forEach(function (element, index) { //using backtick html += ` <div class="noteCard my-2 mx-2 card" style="width: 18rem;"> <div class="card-body"> <h5 class="card-title">Note ${index + 1}</h5> <p class="card-text">${element}</p> <button href="#" class="btn btn-primary">Delete Note</button> </div> </div>`; }); let notes_element = document.getElementById("notes"); if (notesObj.length != 0) { notes_element.innerHTML = html; //console.log("Add notes success!") } else {//default note notes_element.innerHTML = `No note added. Use "Add Note" button to add a note.`; //console.log('Notes not added!'); } }
帶有 id 'notes' 的 div 不會在 javascript 代碼中顯示默認注釋
蕪湖不蕪
2021-11-25 16:12:08