在本地存储中保存数据。

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

Saving data in the local storage

问题

我使用了HTML和JavaScript编写了一个简单的待办事项列表代码。但每次刷新代码时,列表都会被删除。我了解了本地存储的概念,但无论我如何尝试,都无法在我的代码中实现这个概念。以下是我的代码。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <h1>ToDo list</h1>
    <div class="addNewWork">
        <input type="text" placeholder="Enter new work">
        <button>Enter</button>
    </div>
    <ul>

        <li class="random">Notebook</li>
        <li>Jello</li>
        <li>Spinach</li>
        <li>Rice</li>
        <li>Birthday Cake</li>
        <li>Candles</li>
    </ul>
    <script type="text/javascript" src="script.js"></script>
</body>
</html>
var button = document.querySelector("button");
var input = document.querySelector("input");
var ul = document.querySelector("ul");

function inputValueCheck() {
    return input.value.length;
};

function addElement() {
    var li = document.createElement("li");
    li.appendChild(document.createTextNode(input.value));
    ul.appendChild(li)
    input.value = "";
};

function mouseClickEvent() {
    if (inputValueCheck() > 0) {
        addElement();
    };
};

function keypressEvent(event) {
    if (inputValueCheck() > 0 && event.code === "Enter") {
        addElement();
    };
};

button.addEventListener("click", mouseClickEvent);

input.addEventListener("keypress", keypressEvent);

请有人演示如何将输入的数据保存到本地存储并将其作为代码中的列表标签访问,或者提供一些提示,以便我可以自己解决它?

这是我尝试的方式。我能够将数据保存到本地存储,但无法正确实现localStorage.getItem()方法。以下是我如何将数据保存到本地存储的方式。

var button = document.querySelector("button");
var input = document.querySelector("input");
var ul = document.querySelector("ul");
var workItems = [];

function inputValueCheck() {
    return input.value.length;
};

function saveDataInLocalStorage() {
    workItems.push(input.value);
    localStorage.setItem("items", JSON.stringify(workItems));
}

function addElement() {
    saveDataInLocalStorage();
    var li = document.createElement("li");
    li.appendChild(document.createTextNode(input.value));
    ul.appendChild(li)
    input.value = "";
};

function mouseClickEvent() {
    if (inputValueCheck() > 0) {
        addElement();
    };
};

function keypressEvent(event) {
    if (inputValueCheck() > 0 && event.code === "Enter") {
        addElement();
    };
};

button.addEventListener("click", mouseClickEvent);

input.addEventListener("keypress", keypressEvent);

希望这对你有所帮助。如果你需要更多的解释或指导,请随时告诉我。

英文:

I wrote a simple To do list code with html and java script. But every time I refresh the code the list are being deleted. I came to know about local storage. But no matter how much I try I cannot implement the concept in my code. Here is my code.

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Document&lt;/title&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1&gt;ToDo list&lt;/h1&gt;
    &lt;div class=&quot;addNewWork&quot;&gt;
        &lt;input type=&quot;text&quot; placeholder=&quot;Enter new work&quot;&gt;
        &lt;button&gt;Enter&lt;/button&gt;
    &lt;/div&gt;
    &lt;ul&gt;

        &lt;li class=&quot;random&quot;&gt;Notebook&lt;/li&gt;
        &lt;li&gt;Jello&lt;/li&gt;
        &lt;li&gt;Spinach&lt;/li&gt;
        &lt;li&gt;Rice&lt;/li&gt;
        &lt;li&gt;Birthday Cake&lt;/li&gt;
        &lt;li&gt;Candles&lt;/li&gt;
    &lt;/ul&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;script.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
var button = document.querySelector(&quot;button&quot;);
var input = document.querySelector(&quot;input&quot;);
var ul = document.querySelector(&quot;ul&quot;);

function inputValueCheck() {
    return input.value.length;
};

function addElement() {
    var li = document.createElement(&quot;li&quot;);
    li.appendChild(document.createTextNode(input.value));
    ul.appendChild(li)
    input.value = &quot;&quot;;
};

function mouseClickEvent() {
    if (inputValueCheck() &gt; 0) {
        addElement();
    };
};

function keypressEvent(event) {
    if (inputValueCheck() &gt; 0 &amp;&amp; event.code === &quot;Enter&quot;) {
        addElement();
    };
};

button.addEventListener(&quot;click&quot;, mouseClickEvent);

input.addEventListener(&quot;keypress&quot;, keypressEvent);

Can someone please demonstrate how to save the data from the input to localstorage and access it as a list tag in my code or just give some hints so that I can solve it myself?

Here is how I tried to do it. I was able to save the data to local storage but I couldn't able to implement the method localStorage.getItem() in the right way. Here is how I was able to save the data to local storage.

var button = document.querySelector(&quot;button&quot;);
var input = document.querySelector(&quot;input&quot;);
var ul = document.querySelector(&quot;ul&quot;);
var workItems = [];

function inputValueCheck() {
    return input.value.length;
};

function saveDataInLocalStorage() {
    workItems.push(input.value);
    localStorage.setItem(&quot;items&quot;, JSON.stringify(workItems));
}

function addElement() {
    saveDataInLocalStorage();
    var li = document.createElement(&quot;li&quot;);
    li.appendChild(document.createTextNode(input.value));
    ul.appendChild(li)
    input.value = &quot;&quot;;
};

function mouseClickEvent() {
    if (inputValueCheck() &gt; 0) {
        addElement();
    };
};

function keypressEvent(event) {
    if (inputValueCheck() &gt; 0 &amp;&amp; event.code === &quot;Enter&quot;) {
        addElement();
    };
};

button.addEventListener(&quot;click&quot;, mouseClickEvent);

input.addEventListener(&quot;keypress&quot;, keypressEvent);

答案1

得分: 1

Sure, here's the translated code:

首先,将添加元素的函数分离出来。

function addElement () {
    saveDataInLocalStorage();
    addElementToDom(input.value);
    input.value = "";
};

function addElementToDom(text) {
    var li = document.createElement("li");
    li.appendChild(document.createTextNode(text));
    ul.appendChild(li);
}

现在您可以在页面加载时使用 'addElementToDom' 函数将保存在本地存储中的待办事项添加到页面上如下所示

```javascript
const items = JSON.parse(localStorage.getItem("items"));
items.forEach(addElementToDom);

请注意,我已将 HTML 实体字符(例如 &quot;&#39;)转换为它们的对应字符。

英文:

First seperate the add element function.

function addElement () {
    saveDataInLocalStorage();
    addElementToDom(input.value);
    input.value = &quot;&quot;;
};

function addElementToDom(text) {
    var li = document.createElement(&quot;li&quot;);
    li.appendChild(document.createTextNode(text));
    ul.appendChild(li)
}

Now you can use the 'addElementToDom' function on page load to add the to-do's saved in localstorage to the page like so.

const items = JSON.parse(localStorage.getItem(&quot;items&quot;));
items.forEach(addElementToDom);

答案2

得分: 0

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

let workItems = [];
if (JSON.parse(localStorage.getItem("items")) === null) {
  localStorage.setItem("items", JSON.stringify(workItems));
}

function saveDataInLocalStorage() {
    workItems = JSON.parse(localStorage.getItem("items"));
    workItems.push(input.value);
    localStorage.setItem("items", JSON.stringify(workItems));
}

请注意,我已将 HTML 实体引用(如 &quot;)还原为原始的双引号。

英文:
let workItems=[];
if(JSON.parse(localStorage.getItem(&quot;items&quot;) === null ){
  localStorage.setItem(&quot;items&quot;, JSON.stringify(workItems));
}

function saveDataInLocalStorage() {
    workItems= JSON.parse(localStorage.getItem(&quot;items&quot;));
    workItems.push(input.value);
    localStorage.setItem(&quot;items&quot;, JSON.stringify(workItems));
}

huangapple
  • 本文由 发表于 2023年6月13日 18:28:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76463948.html
匿名

发表评论

匿名网友

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

确定