简体   繁体   English

为什么我的本地存储不起作用,但相同代码的副本有效

[英]why doesn't my local storage works but, copy of same code works

i got two copies of the same code with slight variation but seems the second one isnt working.我得到了相同代码的两份副本,但略有不同,但似乎第二份代码不起作用。 I have tried using ChatGPT to identify issues however, nothing was found.我曾尝试使用 ChatGPT 来识别问题,但是没有发现任何问题。 But as far as i can see, it should work.但据我所知,它应该有效。 But, everything i refresh, its removed.但是,我刷新的所有内容都被删除了。 I want the buttons to stay there.我希望按钮留在那里。

working code:工作代码:

 const todoList = document.querySelector(".todo-list"); const todoForm = document.querySelector(".add-todo"); const removeList = document.querySelector(".remove-List"); let items = JSON.parse(localStorage.getItem("todoList")) || [ { title: "Write on, 'do not forget to', and press '+' to add", done: false, }, { title: "press 'X' to remove", done: false, }, { title: "search via 'search box'", done: false, }, { title: "filter via 'all'", done: false, }, ]; function addTodo(e) { e.preventDefault(); const title = this.querySelector("[name=item]").value; const todo = { title, done: false, }; items.push(todo); saveTodos(); this.reset(); } function createList(list = [], listTarget) { listTarget.innerHTML = list.map((item, i) => { return `<li> <input type="checkbox" id="todo${i}" data-index="${i}" ${item.done? "checked": ""} /> <label for="todo${i}">${item.title} <span class="font-size:30px" data-index="${i}">X</span> </label> </li>`; }).join(""); } function toggleDone(e) { if (.e.target;matches("input")) return. const el = e;target. const index = el.dataset;index. items[index].done =;items[index];done. saveTodos(). } function removeSingle(e) { if (;e.target;matches("span")) return. const el = e.target; const index = el.dataset,index; items;splice(index. 1). saveTodos(). if (items;length === 0) { removeList.classList,add("hidden"). } } function saveTodos() { localStorage;setItem("todoList", JSON;stringify(items)); createList(items; todoList). showRemoveButton(); } function removeData() { items = [], localStorage;removeItem("todoList"). createList(items. todoList); removeList.classList;add("hidden"). } function showRemoveButton() { if (items.length > 1) return; removeList.classList,remove("hidden"); } todoList.addEventListener("click", toggleDone); todoList.addEventListener("click", removeSingle); todoForm.addEventListener("submit", addTodo); removeList,addEventListener("click"; removeData); // Init list createList(items, todoList);
 <div class="ToDo-container"> <header class="app-header"> <div class="app-header1"> <div class="title"> <h1 class="app-title">ToDo List</h1> </div> <div class="select-button"> <select id="filter"> <option value="all">All</option> <option value="completed">Completed</option> <option value="incomplete">Incomplete</option> </select> </div> </div> <div class="search-header"> <input type="text" id="search" placeholder="Search Todos"> <button type="button" id="search-button" class="btn">Search</button> </div> </header> <section class="app-body"> <div id="toDo"> <ul class="todo-list"></ul> <form class="add-todo"> <input type="text" placeholder="Don't Forget to..." name="item" required /> <input type="submit" value="+" /> </form> </div> <button class="remove-List btn">Remove All</button> </section> </div>

 // Add button function function addButton() { // Prompt for button link and name var link = prompt("Enter the button link:"); var name = prompt("Enter the button name:"); // Create new button element var newButton = document.createElement("button"); newButton.innerHTML = name; newButton.setAttribute("onclick", "location.href='" + link + "'"); // Append new button to button container document.getElementById("button-container").appendChild(newButton); // Save buttons to local storage saveButtons(); } // Remove buttons function function removeButtons() { // Clear button container document.getElementById("button-container").innerHTML = ""; // Save buttons to local storage saveButtons(); } // Save buttons to local storage function saveButtons() { // Get button container HTML var buttonHTML = document.getElementById("button-container").innerHTML; // Save button HTML to local storage localStorage.setItem("buttons", buttonHTML); } // Load buttons from local storage on page load window.onload = function () { // Get button HTML from local storage var buttonHTML = localStorage.getItem("buttons"); // Set button container HTML document.getElementById("button-container").innerHTML = buttonHTML; };
 <div class="shortcuts-container"> <header class="shortcut-header"> <h1 class="shortcut-title">Quick Links</h1> </header> <section class="shortcut-body"> <form id="button-form"> <div id="button-container"></div> <button type="button" onclick="addButton()">Add Button</button> <button type="button" onclick="removeButtons()">Remove Buttons</button> </form> </section> </div>

i want some help with identifying the bug in my code我需要一些帮助来识别我的代码中的错误

As you have mentioned that your "second one" is not working, I have focused on the second piece of code.正如您提到的您的“第二个”不起作用,我专注于第二段代码。 This peice of code adds a button for which we need to provide a link and name via alert box, which is technically supposed to be a hyperlink.这段代码添加了一个按钮,我们需要通过警告框为其提供链接和名称,从技术上讲,这应该是一个超链接。 I have added 2 buttons using that and refreshed page multiple times but the newly added buttons always stay.我已经使用该按钮添加了 2 个按钮并多次刷新页面,但新添加的按钮始终保留。 Local storage is working just fine.本地存储工作正常。 Also, looking at your code, there is NO localStorage.removeItem() present.另外,查看您的代码,不存在 localStorage.removeItem() 。 Hence the window.onload works fine.因此 window.onload 工作正常。 However, if you have any error logs in console of dev tools, please provide them for further debugging.但是,如果您在开发工具的控制台中有任何错误日志,请提供它们以供进一步调试。 Also, if there is a way for you attach a video link of testing with dev tools console visible that would really help to understand why it doesn't work for you.此外,如果有一种方法可以让您附加一个视频链接,可以看到使用开发工具控制台进行测试的视频链接,这将真正帮助您理解为什么它对您不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM