Я пытался сохранить заметки в локальном хранилище разными способами, но, что бы я ни пробовал, ничего не работает должным образом. Я изучил несколько подходов и попробовал разные методы, но ни один из них не дал мне тех результатов, на которые я надеялся. Я также решил обратиться за помощью к ИИ, чтобы посмотреть, сможет ли он мне помочь. К сожалению, хотя он и предоставил некоторые предложения, решения не решили проблему. Я дошел до того, что все еще пытаюсь разобраться в этом и действительно нуждаюсь в четком, простом и пошаговом руководстве, чтобы решить эту проблему раз и навсегда.
let note_list=document.getElementById("note_list_container")
function addNote(){
let input_note=document.getElementById("note").value
if(input_note){
let copyButton=document.createElement("button")
copyButton.className="copy_button"
copyButton.innerHTML=``
copyButton.onclick=function copyContent(){
navigator.clipboard.writeText(input_note) //creating copy paste button !
}
let deleteButton=document.createElement("button")
deleteButton.className="delete_button"
deleteButton.innerHTML=``
deleteButton.onclick=function deleteNote(){
display_note.style.display="none" //creating delete note button, the delete button is created further down the page but its still working !
}
let editButton=document.createElement("button")
editButton.innerHTML="edit"
let display_note=document.createElement("p")
display_note.className="note_info" //appeding each button to our note
display_note.textContent=input_note
note_list.appendChild(display_note)
display_note.appendChild(deleteButton)
display_note.appendChild(copyButton)
document.getElementById("note").value=""
display_note.appendChild(editButton);
//creating a variable that will hold the localstorage
//checks if there are any notes saved, if not it generates the localstorage
let notes=localStorage.getItem("notes") ? JSON.parse(localStorage.getItem("notes")) : []
notes.push(note_list.innerHTML)
localStorage.setItem("notes", JSON.stringify(notes))
console.log(localStorage.getItem("notes"))
//creating the logic for the edit //tried this but doesnt seem to work
editButton.onclick=function(){ //logic for editing the note and than saving the change
let edit_form=document.createElement("input")
edit_form.value=display_note.textContent
edit_form.type="text"
let save=document.createElement("button")
save.textContent="save"
save.onclick=function(){
display_note.textContent=edit_form.value
display_note.appendChild(deleteButton)
display_note.appendChild(copyButton)
display_note.appendChild(editButton)
}
display_note.innerHTML="" //emptying the html of the form so only the edit form and the save button appears
display_note.appendChild(edit_form)
display_note.appendChild(save)
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... e-using-js
Невозможно сохранить заметки в localStorage с помощью JS. ⇐ Javascript
Форум по Javascript
-
Anonymous
1736798540
Anonymous
Я пытался сохранить заметки в локальном хранилище разными способами, но, что бы я ни пробовал, ничего не работает должным образом. Я изучил несколько подходов и попробовал разные методы, но ни один из них не дал мне тех результатов, на которые я надеялся. Я также решил обратиться за помощью к ИИ, чтобы посмотреть, сможет ли он мне помочь. К сожалению, хотя он и предоставил некоторые предложения, решения не решили проблему. Я дошел до того, что все еще пытаюсь разобраться в этом и действительно нуждаюсь в четком, простом и пошаговом руководстве, чтобы решить эту проблему раз и навсегда.
let note_list=document.getElementById("note_list_container")
function addNote(){
let input_note=document.getElementById("note").value
if(input_note){
let copyButton=document.createElement("button")
copyButton.className="copy_button"
copyButton.innerHTML=``
copyButton.onclick=function copyContent(){
navigator.clipboard.writeText(input_note) //creating copy paste button !
}
let deleteButton=document.createElement("button")
deleteButton.className="delete_button"
deleteButton.innerHTML=``
deleteButton.onclick=function deleteNote(){
display_note.style.display="none" //creating delete note button, the delete button is created further down the page but its still working !
}
let editButton=document.createElement("button")
editButton.innerHTML="edit"
let display_note=document.createElement("p")
display_note.className="note_info" //appeding each button to our note
display_note.textContent=input_note
note_list.appendChild(display_note)
display_note.appendChild(deleteButton)
display_note.appendChild(copyButton)
document.getElementById("note").value=""
display_note.appendChild(editButton);
//creating a variable that will hold the localstorage
//checks if there are any notes saved, if not it generates the localstorage
let notes=localStorage.getItem("notes") ? JSON.parse(localStorage.getItem("notes")) : []
notes.push(note_list.innerHTML)
localStorage.setItem("notes", JSON.stringify(notes))
console.log(localStorage.getItem("notes"))
//creating the logic for the edit //tried this but doesnt seem to work
editButton.onclick=function(){ //logic for editing the note and than saving the change
let edit_form=document.createElement("input")
edit_form.value=display_note.textContent
edit_form.type="text"
let save=document.createElement("button")
save.textContent="save"
save.onclick=function(){
display_note.textContent=edit_form.value
display_note.appendChild(deleteButton)
display_note.appendChild(copyButton)
display_note.appendChild(editButton)
}
display_note.innerHTML="" //emptying the html of the form so only the edit form and the save button appears
display_note.appendChild(edit_form)
display_note.appendChild(save)
}
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79353294/cant-save-notes-in-localstorage-using-js[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия