[英]Is there a way to save an HTML list (UL, LI) to a file, and then retrieve, and display it later?
I have a "todo list" (mostly "borrowed" from W3) where the user can enter data, and the data is then listed as a "li" object on the site. 我有一个“待办事项列表”(主要是从W3借来的),用户可以在其中输入数据,然后数据在站点上被列为“ li”对象。 I want to store the data that the user inputs to a file, so that if the user refreshes, the items on the list will re-appear as they were.
我想将用户输入的数据存储到文件中,以便用户刷新时,列表中的项目将重新显示。
I'm running this locally, so I preferably want to just store it in a textfile with separators. 我正在本地运行它,所以我最好只将其存储在带有分隔符的文本文件中。
I have tried to use some JS to get all the items with the tagname "li", but i haven't messed with JS in a long time, and im pretty rubbish. 我试图使用一些JS来获取所有带有标签名“ li”的项目,但是我已经很长时间没有弄乱JS了,我觉得这很垃圾。 You can see what i tried below.
您可以在下面查看我尝试过的内容。
Javascript: Javascript:
var arrayList =
document.getElementById("myUL").getElementsByTagName("li");
for (i=0; i<arrayList.length;i++) {
alert(arrayList[0].innerHTML);
}
}
HTML list: HTML清单:
<h1>Todo</h1>
<div id="myDIV" class="header">
<input type="text" id="myInput" onkeydown = "if (event.keyCode == 13) document.getElementById('addBtn').click()" placeholder="Task...">
<span onclick="newElement(), listGet()" id="addBtn" class="addBtn">Add</span>
</div>
<ul id="myUL">
</ul>
</div>
This is the alert I get when I enter some text: 这是我输入一些文本时收到的警报:
"Do the dishes<span class="close">×</span>"
I would optimally want to only get the text input, then somehow store the information, and display it the next time I refresh the website. 我最好只获取文本输入,然后以某种方式存储信息,并在下次刷新网站时显示它。
Please let me know if I need to change anything in my question, or if my question is too open. 如果我需要更改问题中的任何内容,或者我的问题过于公开,请告诉我。 I assume I should only ask questions that have to do with code that is written wrong, not about code that isn't written yet, but I really need someone to point me in the right direction, so its worth a shot.. Thanks!
我想我只应该问与编写错误的代码有关的问题,而不是与尚未编写的代码有关的问题,但是我真的需要有人指出正确的方向,因此值得一试。.谢谢!
Using a button click you can store values in Local Storage like: 使用按钮单击可以将值存储在本地存储中,例如:
<h1>Todo</h1>
<div id="myDIV" class="header">
<div>
<input type="text" id="myInput" onkeydown="if(event.keyCode == 13) placeholder="Task...">
<button onclick="newElement(), listGet()" id="addBtn" class="addBtn">Add</button>
</div>
<ul id="myUL">
</ul>
</div>
<script type="text/javascript">
function newElement(){
var inputData= document.getElementById("myInput");
localStorage.setItem("data", inputData.value); // store values using setItem method
}
</script>
// Get the value from Local Storage
var storedData = localStorage.getItem("data");
What are newElement()
& listGet()
? 什么是
newElement()
和listGet()
?
You can get your li
nodes by using getElementsByTagName()
: 您可以使用
getElementsByTagName()
获得li
节点:
let yourUL = document.getElementById('myUL');
let yourLiNodeList = yourUL.getElementsByTagName('li');
console.log(yourLiNodeList);
Here's a fiddle with a demo :) 这是一个演示的小提琴 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.