检索JavaScript中的本地存储值。

huangapple go评论73阅读模式
英文:

Retrieve local storage value in Javscript

问题

以下是您提供的代码的中文翻译:

我能够创建列表,编辑列表和删除列表。我还在函数中创建了本地存储,但是当我在浏览器中按下刷新时,列表会消失(这意味着我无法检索列表)。我希望列表在我关闭浏览器后仍然保留。

window.addEventListener('load', () => {
  const form = document.querySelector("#new-task-form");
  const input = document.querySelector("#new-task-input");
  const list_el = document.querySelector("#tasks");

  form.addEventListener('submit', (e) => {
    e.preventDefault();

    const task = input.value;

    const task_el = document.createElement('div');
    task_el.classList.add('task');

    const task_content_el = document.createElement('div');
    task_content_el.classList.add('content');

    task_el.appendChild(task_content_el);

    const task_input_el = document.createElement('input');
    task_input_el.classList.add('text');
    task_input_el.type = 'text';
    task_input_el.value = task;
    task_input_el.setAttribute('readonly', 'readonly');

    task_content_el.appendChild(task_input_el);

    const task_actions_el = document.createElement('div');
    task_actions_el.classList.add('actions');
    localStorage.setItem('tasks', JSON.stringify(task))

    const storedTask = JSON.parse(localStorage.getItem('tasks'));

    const task_edit_el = document.createElement('button');
    task_edit_el.classList.add('edit');
    task_edit_el.innerText = 'Edit';

    const task_delete_el = document.createElement('button');
    task_delete_el.classList.add('delete');
    task_delete_el.innerText = 'Delete';

    task_actions_el.appendChild(task_edit_el);
    task_actions_el.appendChild(task_delete_el);

    task_el.appendChild(task_actions_el);

    list_el.appendChild(task_el);

    input.value = '';

    task_edit_el.addEventListener('click', (e) => {
      if (task_edit_el.innerText.toLowerCase() == "edit") {
        task_edit_el.innerText = "Save";
        task_input_el.removeAttribute("readonly");
        task_input_el.focus();
      } else {
        task_edit_el.innerText = "Edit";
        task_input_el.setAttribute("readonly", "readonly");
      }
    });

    task_delete_el.addEventListener('click', (e) => {
      list_el.removeChild(task_el);
    });
  });
});
:root {
  --dark: #374151;
  --darker: #1F2937;
  --darkest: #111827;
  --grey: #6B7280;
  --pink: #EC4899;
  --purple: #8B5CF6;
  --light: #EEE;
}

* {
  margin: 0;
  box-sizing: border-box;
  font-family: "Fira sans", sans-serif;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  color: #FFF;
  background-color: var(--dark);
}

header {
  padding: 2rem 1rem;
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
}

header h1 {
  font-size: 2.5rem;
  font-weight: 300;
  color: var(--grey);
  margin-bottom: 1rem;
}

#new-task-form {
  display: flex;
}

input,
button {
  appearance: none;
  border: none;
  outline: none;
  background: none;
}

#new-task-input {
  flex: 1 1 0%;
  background-color: var(--darker);
  padding: 1rem;
  border-radius: 1rem;
  margin-right: 1rem;
  color: var(--light);
  font-size: 1.25rem;
}

#new-task-input::placeholder {
  color: var(--grey);
}

#new-task-submit {
  color: var(--pink);
  font-size: 1.25rem;
  font-weight: 700;
  background-image: linear-gradient(to right, var(--pink), var(--purple));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  cursor: pointer;
  transition: 0.4s;
}

#new-task-submit:hover {
  opacity: 0.8;
}

#new-task-submit:active {
  opacity: 0.6;
}

main {
  flex: 1 1 0%;
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
}

.task-list {
  padding: 1rem;
}

.task-list h2 {
  font-size: 1.5rem;
  font-weight: 300;
  color: var(--grey);
  margin-bottom: 1rem;
}

#tasks .task {
  display: flex;
  justify-content: space-between;
  background-color: var(--darkest);
  padding: 1rem;
  border-radius: 1rem;
  margin-bottom: 1rem;
}

.task .content {
  flex: 1 1 0%;
}

.task .content .text {
  color: var(--light);
  font-size: 1.125rem;
  width: 100%;
  display: block;
  transition: 0.4s;
}

.task .content .text:not(:read-only) {
  color: var(--pink);
}

.task .actions {
  display: flex;
  margin: 0 -0.5rem;
}

.task .actions button {
  cursor: pointer;
  margin: 0 0.5rem;
  font-size: 1.125rem;
  font-weight: 700;
  text-transform: uppercase;
  transition: 0.4s;
}

.task .actions button:hover {
  opacity: 0.8;
}

.task .actions button:active {
  opacity: 0.6;
}

.task .actions .edit {
  background-image: linear-gradient(to right, var(--pink), var(--purple));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.task .actions .delete {
  color: crimson;
}
<header>
  <h1>任务列表 2021</h1>
  <form id="new-task-form">
    <input type="text" name="new-task-input" id="new-task-input" placeholder="你有什么计划?" />
    <input type="submit" id="new-task-submit" value="添加任务" />
  </form>
</header>
<main>
  <section class="task-list">
    <h2>任务</h2>
    <div id="tasks">
    </div>
  </section>
</main>
英文:

I am able to create list, edit list and delete list. I also created localstorage in the function, however when I press refresh in the browser the lists are gone( which means I couldn't retrieve the list). I want the list to stay even after i close the browser.

<!-- language: lang-js -->

window.addEventListener(&#39;load&#39;, () =&gt; {
const form = document.querySelector(&quot;#new-task-form&quot;);
const input = document.querySelector(&quot;#new-task-input&quot;);
const list_el = document.querySelector(&quot;#tasks&quot;);
form.addEventListener(&#39;submit&#39;, (e) =&gt; {
e.preventDefault();
const task = input.value;
const task_el = document.createElement(&#39;div&#39;);
task_el.classList.add(&#39;task&#39;);
const task_content_el = document.createElement(&#39;div&#39;);
task_content_el.classList.add(&#39;content&#39;);
task_el.appendChild(task_content_el);
const task_input_el = document.createElement(&#39;input&#39;);
task_input_el.classList.add(&#39;text&#39;);
task_input_el.type = &#39;text&#39;;
task_input_el.value = task;
task_input_el.setAttribute(&#39;readonly&#39;, &#39;readonly&#39;);
task_content_el.appendChild(task_input_el);
const task_actions_el = document.createElement(&#39;div&#39;);
task_actions_el.classList.add(&#39;actions&#39;);
localStorage.setItem(&#39;tasks&#39;, JSON.stringify(task))
const storedTask = JSON.parse(localStorage.getItem(&#39;tasks&#39;));
const task_edit_el = document.createElement(&#39;button&#39;);
task_edit_el.classList.add(&#39;edit&#39;);
task_edit_el.innerText = &#39;Edit&#39;;
const task_delete_el = document.createElement(&#39;button&#39;);
task_delete_el.classList.add(&#39;delete&#39;);
task_delete_el.innerText = &#39;Delete&#39;;
task_actions_el.appendChild(task_edit_el);
task_actions_el.appendChild(task_delete_el);
task_el.appendChild(task_actions_el);
list_el.appendChild(task_el);
input.value = &#39;&#39;;
task_edit_el.addEventListener(&#39;click&#39;, (e) =&gt; {
if (task_edit_el.innerText.toLowerCase() == &quot;edit&quot;) {
task_edit_el.innerText = &quot;Save&quot;;
task_input_el.removeAttribute(&quot;readonly&quot;);
task_input_el.focus();
} else {
task_edit_el.innerText = &quot;Edit&quot;;
task_input_el.setAttribute(&quot;readonly&quot;, &quot;readonly&quot;);
}
});
task_delete_el.addEventListener(&#39;click&#39;, (e) =&gt; {
list_el.removeChild(task_el);
});
});
});

<!-- language: lang-css -->

:root {
--dark: #374151;
--darker: #1F2937;
--darkest: #111827;
--grey: #6B7280;
--pink: #EC4899;
--purple: #8B5CF6;
--light: #EEE;
}
* {
margin: 0;
box-sizing: border-box;
font-family: &quot;Fira sans&quot;, sans-serif;
}
body {
display: flex;
flex-direction: column;
min-height: 100vh;
color: #FFF;
background-color: var(--dark);
}
header {
padding: 2rem 1rem;
max-width: 800px;
width: 100%;
margin: 0 auto;
}
header h1 {
font-size: 2.5rem;
font-weight: 300;
color: var(--grey);
margin-bottom: 1rem;
}
#new-task-form {
display: flex;
;
}
input,
button {
appearance: none;
border: none;
outline: none;
background: none;
}
#new-task-input {
flex: 1 1 0%;
background-color: var(--darker);
padding: 1rem;
border-radius: 1rem;
margin-right: 1rem;
color: var(--light);
font-size: 1.25rem;
}
#new-task-input::placeholder {
color: var(--grey);
}
#new-task-submit {
color: var(--pink);
font-size: 1.25rem;
font-weight: 700;
background-image: linear-gradient(to right, var(--pink), var(--purple));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
cursor: pointer;
transition: 0.4s;
}
#new-task-submit:hover {
opacity: 0.8;
}
#new-task-submit:active {
opacity: 0.6;
}
main {
flex: 1 1 0%;
max-width: 800px;
width: 100%;
margin: 0 auto;
}
.task-list {
padding: 1rem;
}
.task-list h2 {
font-size: 1.5rem;
font-weight: 300;
color: var(--grey);
margin-bottom: 1rem;
}
#tasks .task {
display: flex;
justify-content: space-between;
background-color: var(--darkest);
padding: 1rem;
border-radius: 1rem;
margin-bottom: 1rem;
}
.task .content {
flex: 1 1 0%;
}
.task .content .text {
color: var(--light);
font-size: 1.125rem;
width: 100%;
display: block;
transition: 0.4s;
}
.task .content .text:not(:read-only) {
color: var(--pink);
}
.task .actions {
display: flex;
margin: 0 -0.5rem;
}
.task .actions button {
cursor: pointer;
margin: 0 0.5rem;
font-size: 1.125rem;
font-weight: 700;
text-transform: uppercase;
transition: 0.4s;
}
.task .actions button:hover {
opacity: 0.8;
}
.task .actions button:active {
opacity: 0.6;
}
.task .actions .edit {
background-image: linear-gradient(to right, var(--pink), var(--purple));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.task .actions .delete {
color: crimson;
}

<!-- language: lang-html -->

&lt;header&gt;
&lt;h1&gt;Task List 2021&lt;/h1&gt;
&lt;form id=&quot;new-task-form&quot;&gt;
&lt;input type=&quot;text&quot; name=&quot;new-task-input&quot; id=&quot;new-task-input&quot; placeholder=&quot;What do you have planned?&quot; /&gt;
&lt;input type=&quot;submit&quot; id=&quot;new-task-submit&quot; value=&quot;Add task&quot; /&gt;
&lt;/form&gt;
&lt;/header&gt;
&lt;main&gt;
&lt;section class=&quot;task-list&quot;&gt;
&lt;h2&gt;Tasks&lt;/h2&gt;
&lt;div id=&quot;tasks&quot;&gt;
&lt;/div&gt;
&lt;/section&gt;
&lt;/main&gt;

<!-- end snippet -->

答案1

得分: 1

你可以使用 localStorage.getItem('yourKey') 检索本地存储项,并使用 localStorage.setItem('yourKey', yourValue) 存储它们。

const value = 'my_test_value';
const key = 'my_key';

localStorage.setItem(key, value); // 将值存储在本地存储中
console.log(localStorage.getItem(key)); // 从本地存储中获取值
英文:

You can retrieve local storage items using localStorage.getItem(&#39;yourKey&#39;) and store them using localStorage.setItem(&#39;yourKey&#39;, youValue)

const value = &#39;my_test_value&#39;;
const key = &#39;my_key&#39;;
localStorage.setItem(key, value ); // set value in local storage
console.log(localStorage.getItem(key));  // get value in local storage

答案2

得分: 1

你将存储的任务放在了错误的位置,并且实际上没有使用它们。
你可能想要这样做:

我没有在这里进行测试,因为我无法在 Stack Overflow 上测试,但思路是正确的。

const tasks = JSON.parse(localStorage.getItem('tasks') || '[]'); // 获取任务或创建一个空数组
const saveList = () => localStorage.setItem('tasks', JSON.stringify(tasks));

window.addEventListener('load', () => {
  const form = document.querySelector("#new-task-form");
  const input = document.querySelector("#new-task-input");
  const list_el = document.querySelector("#tasks");

  form.addEventListener('submit', (e) => {
    e.preventDefault();

    const task = input.value;

    const task_el = document.createElement('div');
    task_el.classList.add('task');

    const task_content_el = document.createElement('div');
    task_content_el.classList.add('content');

    task_el.appendChild(task_content_el);

    const task_input_el = document.createElement('input');
    task_input_el.classList.add('text');
    task_input_el.type = 'text';
    task_input_el.value = task;
    task_input_el.setAttribute('readonly', 'readonly');

    task_content_el.appendChild(task_input_el);

    const task_actions_el = document.createElement('div');
    task_actions_el.classList.add('actions');
    tasks.push(task);
    saveList();
    const task_edit_el = document.createElement('button');
    task_edit_el.classList.add('edit');
    task_edit_el.innerText = 'Edit';

    const task_delete_el = document.createElement('button');
    task_delete_el.classList.add('delete');
    task_delete_el.innerText = 'Delete';

    task_actions_el.appendChild(task_edit_el);
    task_actions_el.appendChild(task_delete_el);

    task_el.appendChild(task_actions_el);

    list_el.appendChild(task_el);

    input.value = '';
  });

  list_el.addEventListener('click', (e) => {
    const tgt = e.target;
    const task_el = tgt.closest(".task")
    const task_input_el = task_el.querySelector(".text");
    if (tgt.matches(".edit")) {
      if (tgt.innerText === "Edit") {
        tgt.innerText = "Save";
        task_input_el.removeAttribute("readonly");
        task_input_el.focus();
      } else {
        tgt.innerText = "Edit";
        task_input_el.setAttribute("readonly", "readonly");
      }
    } else if (tgt.matches("delete")) {
      list_el.removeChild(task_el);
      tasks = tasks.filter(task => task != task_el.value);
      saveList();
    }
  });
});
英文:

You get the storedTasks in the wrong place and do not actually use them
You likely want this

I did not test it since I cannot here at SO, but the idea is correct

<!-- language: lang-js -->

const tasks = JSON.parse(localStorage.getItem(&#39;tasks&#39;) || &#39;[]&#39;); // get the tasks or create an empty array
const saveList = () =&gt;     localStorage.setItem(&#39;tasks&#39;, JSON.stringify(tasks));
window.addEventListener(&#39;load&#39;, () =&gt; {
const form = document.querySelector(&quot;#new-task-form&quot;);
const input = document.querySelector(&quot;#new-task-input&quot;);
const list_el = document.querySelector(&quot;#tasks&quot;);
form.addEventListener(&#39;submit&#39;, (e) =&gt; {
e.preventDefault();
const task = input.value;
const task_el = document.createElement(&#39;div&#39;);
task_el.classList.add(&#39;task&#39;);
const task_content_el = document.createElement(&#39;div&#39;);
task_content_el.classList.add(&#39;content&#39;);
task_el.appendChild(task_content_el);
const task_input_el = document.createElement(&#39;input&#39;);
task_input_el.classList.add(&#39;text&#39;);
task_input_el.type = &#39;text&#39;;
task_input_el.value = task;
task_input_el.setAttribute(&#39;readonly&#39;, &#39;readonly&#39;);
task_content_el.appendChild(task_input_el);
const task_actions_el = document.createElement(&#39;div&#39;);
task_actions_el.classList.add(&#39;actions&#39;);
tasks.push(task);
saveList();
const task_edit_el = document.createElement(&#39;button&#39;);
task_edit_el.classList.add(&#39;edit&#39;);
task_edit_el.innerText = &#39;Edit&#39;;
const task_delete_el = document.createElement(&#39;button&#39;);
task_delete_el.classList.add(&#39;delete&#39;);
task_delete_el.innerText = &#39;Delete&#39;;
task_actions_el.appendChild(task_edit_el);
task_actions_el.appendChild(task_delete_el);
task_el.appendChild(task_actions_el);
list_el.appendChild(task_el);
input.value = &#39;&#39;;
});
list_el.addEventListener(&#39;click&#39;, (e) =&gt; {
const tgt = e.target;
const task_el = tgt.closest(&quot;.task&quot;)
const task_input_el = task_el.querySelector(&quot;.text&quot;);
if (tgt.matches(&quot;.edit&quot;)) {
if (tgt.innerText === &quot;Edit&quot;) {
tgt.innerText = &quot;Save&quot;;
task_input_el.removeAttribute(&quot;readonly&quot;);
task_input_el.focus();
} else {
tgt.innerText = &quot;Edit&quot;;
task_input_el.setAttribute(&quot;readonly&quot;, &quot;readonly&quot;);
}
} else if (tgt.matches(&quot;delete&quot;)) {
list_el.removeChild(task_el);
tasks = tasks.filter(task =&gt; task != task_el.value);
saveList();
}
});
});

<!-- end snippet -->

答案3

得分: 1

以下是您要翻译的代码部分:

Something that you seem to miss is, only saving to localStorage doesn't guarantee that you are gonna get it back on refresh, but you should have to manually fetch and populate your HTML from your localStorage, on page load.

Simply paste this code into your main.js, it should work fine.
I have added a lot of comments and I think they are helpful.

// Pre-populating your HTML on page loading
function populateStoredTasks(storedTasks) {
  // Taking the parent item
  const list_el = document.querySelector("#tasks");

  // Map over your stored tasks array and populate your #tasks field
  storedTasks.forEach(function (task) {
    // Much code duplication here but you can fix it once you get the idea
    const task_el = document.createElement('div');
    task_el.classList.add('task');

    const task_content_el = document.createElement('div');
    task_content_el.classList.add('content');

    task_el.appendChild(task_content_el);

    const task_input_el = document.createElement('input');

    task_input_el.classList.add('text');
    task_input_el.type = 'text';
    task_input_el.value = storedTasks[0];
    task_input_el.setAttribute('readonly', 'readonly');

    task_content_el.appendChild(task_input_el);

    const task_actions_el = document.createElement('div');
    task_actions_el.classList.add('actions');

    const task_edit_el = document.createElement('button');
    task_edit_el.classList.add('edit');
    task_edit_el.innerText = 'Edit';

    const task_delete_el = document.createElement('button');
    task_delete_el.classList.add('delete');
    task_delete_el.innerText = 'Delete';

    task_actions_el.appendChild(task_edit_el);
    task_actions_el.appendChild(task_delete_el);

    task_edit_el.addEventListener('click', (e) => {
      if (task_edit_el.innerText.toLowerCase() == "edit") {
        task_edit_el.innerText = "Save";
        task_input_el.removeAttribute("readonly");
        task_input_el.focus();
      } else {
        task_edit_el.innerText = "Edit";
        task_input_el.setAttribute("readonly", "readonly");
      }
    });

    task_delete_el.addEventListener('click', (e) => {
      list_el.removeChild(task_el);
    });

    task_el.appendChild(task_actions_el);

    list_el.appendChild(task_el);
  });
}

window.addEventListener('load', () => {
  const form = document.querySelector("#new-task-form");
  const input = document.querySelector("#new-task-input");
  const list_el = document.querySelector("#tasks");

  // Fetching your storedTasks makes sense here
  // Also, check if your local storage has 'tasks' item
  const storedTasks = localStorage.getItem('tasks') ? JSON.parse(localStorage.getItem('tasks')) : null;

  // Call the function to populate your HTML
  if (localStorage.getItem('tasks')) {
    populateStoredTasks(storedTasks);
  }

  form.addEventListener('submit', (e) => {
    e.preventDefault();

    const task = input.value;

    const task_el = document.createElement('div');
    task_el.classList.add('task');

    const task_content_el = document.createElement('div');
    task_content_el.classList.add('content');

    task_el.appendChild(task_content_el);

    const task_input_el = document.createElement('input');
    task_input_el.classList.add('text');
    task_input_el.type = 'text';
    task_input_el.value = task;
    task_input_el.setAttribute('readonly', 'readonly');

    task_content_el.appendChild(task_input_el);

    const task_actions_el = document.createElement('div');
    task_actions_el.classList.add('actions');

    // Tasks should be stored in an array in your local storage
    // Check if your tasks in localStorage are empty
    if (storedTasks) {
      localStorage.setItem('tasks', JSON.stringify([...storedTasks, task]));
    } else {
      localStorage.setItem('tasks', JSON.stringify([task]));
    }

    const task_edit_el = document.createElement('button');
    task_edit_el.classList.add('edit');
    task_edit_el.innerText = 'Edit';

    const task_delete_el = document.createElement('button');
    task_delete_el.classList.add('delete');
    task_delete_el.innerText = 'Delete';

    task_actions_el.appendChild(task_edit_el);
    task_actions_el.appendChild(task_delete_el);

    task_el.appendChild(task_actions_el);

    list_el.appendChild(task_el);

    input.value = '';

    task_edit_el.addEventListener('click', (e) => {
      if (task_edit_el.innerText.toLowerCase() == "edit") {
        task_edit_el.innerText = "Save";
        task_input_el.removeAttribute("readonly");
        task_input_el.focus();
      } else {
        task_edit_el.innerText = "Edit";
        task_input_el.setAttribute("readonly", "readonly");
      }
    });

    task_delete_el.addEventListener('click', (e) => {
      list_el.removeChild(task_el);
    });
  });
});

请注意,我已经翻译了您的代码,但保留了原始的标签和代码结构。如果需要更多帮助或有其他问题,请随时提问。

英文:

Something that you seem to miss is, only saving to localStorage doesn't guarantee that you are gonna get it back on refresh, but you should have to manually fetch and populate you html from your localStorage, on page load.

Simply paste this code into you main.js, it should work fine.
I have added a lot of comments and i think they are helpful.

//pre populating yout html on page loading
function populateStoredTasks(storedTasks) {
//taking the parent item
const list_el = document.querySelector(&quot;#tasks&quot;);
//Map over your stored tasks array and populate your #tasks field
storedTasks.forEach(function (task) {
// Much code duplication here but you can fix it once you get the 					idea    
const task_el = document.createElement(&#39;div&#39;);
task_el.classList.add(&#39;task&#39;);
const task_content_el = document.createElement(&#39;div&#39;);
task_content_el.classList.add(&#39;content&#39;);
task_el.appendChild(task_content_el);
const task_input_el = document.createElement(&#39;input&#39;);
task_input_el.classList.add(&#39;text&#39;);
task_input_el.type = &#39;text&#39;;
task_input_el.value = storedTasks[0];
task_input_el.setAttribute(&#39;readonly&#39;, &#39;readonly&#39;);
task_content_el.appendChild(task_input_el);
const task_actions_el = document.createElement(&#39;div&#39;);
task_actions_el.classList.add(&#39;actions&#39;);
const task_edit_el = document.createElement(&#39;button&#39;);
task_edit_el.classList.add(&#39;edit&#39;);
task_edit_el.innerText = &#39;Edit&#39;;
const task_delete_el = document.createElement(&#39;button&#39;);
task_delete_el.classList.add(&#39;delete&#39;);
task_delete_el.innerText = &#39;Delete&#39;;
task_actions_el.appendChild(task_edit_el);
task_actions_el.appendChild(task_delete_el);
task_edit_el.addEventListener(&#39;click&#39;, (e) =&gt; {
if (task_edit_el.innerText.toLowerCase() == &quot;edit&quot;) {
task_edit_el.innerText = &quot;Save&quot;;
task_input_el.removeAttribute(&quot;readonly&quot;);
task_input_el.focus();
} else {
task_edit_el.innerText = &quot;Edit&quot;;
task_input_el.setAttribute(&quot;readonly&quot;, &quot;readonly&quot;);
}
});
task_delete_el.addEventListener(&#39;click&#39;, (e) =&gt; {
list_el.removeChild(task_el);
});
task_el.appendChild(task_actions_el);
list_el.appendChild(task_el);
});	
}
window.addEventListener(&#39;load&#39;, () =&gt; {
const form = document.querySelector(&quot;#new-task-form&quot;);
const input = document.querySelector(&quot;#new-task-input&quot;);
const list_el = document.querySelector(&quot;#tasks&quot;);
//fetching your storedTaks makes sense here 
// also check if you local storage has &#39;tasks&#39; item
const storedTasks = localStorage.getItem(&#39;tasks&#39;) ?  JSON.parse(localStorage.getItem(&#39;tasks&#39;)) : null;
// call function to populate your html
if(localStorage.getItem(&#39;tasks&#39;)) {
populateStoredTasks(storedTasks);
}
form.addEventListener(&#39;submit&#39;, (e) =&gt; {
e.preventDefault();
const task = input.value;
const task_el = document.createElement(&#39;div&#39;);
task_el.classList.add(&#39;task&#39;);
const task_content_el = document.createElement(&#39;div&#39;);
task_content_el.classList.add(&#39;content&#39;);
task_el.appendChild(task_content_el);
const task_input_el = document.createElement(&#39;input&#39;);
task_input_el.classList.add(&#39;text&#39;);
task_input_el.type = &#39;text&#39;;
task_input_el.value = task;
task_input_el.setAttribute(&#39;readonly&#39;, &#39;readonly&#39;);
task_content_el.appendChild(task_input_el);
const task_actions_el = document.createElement(&#39;div&#39;);
task_actions_el.classList.add(&#39;actions&#39;);
//tasks should be stored in an array in your local storage 
//check if your tasks in localStorage is empty
if(storedTasks) {
localStorage.setItem(&#39;tasks&#39;, JSON.stringify([...storedTasks, task]))
} else {
localStorage.setItem(&#39;tasks&#39;, JSON.stringify([task]))
}
const task_edit_el = document.createElement(&#39;button&#39;);
task_edit_el.classList.add(&#39;edit&#39;);
task_edit_el.innerText = &#39;Edit&#39;;
const task_delete_el = document.createElement(&#39;button&#39;);
task_delete_el.classList.add(&#39;delete&#39;);
task_delete_el.innerText = &#39;Delete&#39;;
task_actions_el.appendChild(task_edit_el);
task_actions_el.appendChild(task_delete_el);
task_el.appendChild(task_actions_el);
list_el.appendChild(task_el);
input.value = &#39;&#39;;
task_edit_el.addEventListener(&#39;click&#39;, (e) =&gt; {
if (task_edit_el.innerText.toLowerCase() == &quot;edit&quot;) {
task_edit_el.innerText = &quot;Save&quot;;
task_input_el.removeAttribute(&quot;readonly&quot;);
task_input_el.focus();
} else {
task_edit_el.innerText = &quot;Edit&quot;;
task_input_el.setAttribute(&quot;readonly&quot;, &quot;readonly&quot;);
}
});
task_delete_el.addEventListener(&#39;click&#39;, (e) =&gt; {
list_el.removeChild(task_el);
});
});
});

huangapple
  • 本文由 发表于 2023年5月25日 15:41:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76329936.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定