gpt4 book ai didi

javascript - 如何使用javascript在localstorage中存储带有文本和图像的li

转载 作者:太空宇宙 更新时间:2023-11-04 10:48:29 25 4
gpt4 key购买 nike

我用 html 和 javascript 创建了一个购物 list 程序。我是初学者,不知道如何实现 localstorage 来存储 list 元素。我正在插入程序的完整代码,以更好地解释我实际想要存储在本地存储中的内容。它由一个用于输入元素名称的输入字段和一个单击按钮将输入的文本设置为列表元素的按钮组成。列表项然后有两个图像,一个用于将其标记为已完成,另一个用于从列表中删除该元素。因此,购物 list 中的每个 list 元素都包含图像的两个子节点。不知道如何为本地存储执行此操作。请帮忙

var input = document.getElementById('input');
var add = document.getElementById('add');
var list = document.getElementById('list');
var bottom = document.getElementById('bottom');

add.onclick = ShoppingList;

function ShoppingList(){
var li = document.createElement('li');
var imageDone = document.createElement('img');
var imageRemove = document.createElement('img');

imageDone.setAttribute('src', 'https://cdn0.iconfinder.com/data/icons/round-ui-icons/512/tick_green.png');

imageRemove.setAttribute('src', 'http://findicons.com/files/icons/1580/devine_icons_part_2/128/trash_recyclebin_empty_closed.png');

li.textContent = input.value;
li.appendChild(imageRemove);
li.appendChild(imageDone);
bottom.appendChild(li);
input.value = "";
input.focus();
imageDone.onclick = function(){
li.removeChild(imageRemove);
};
imageRemove.onclick = function(){
bottom.removeChild(li);
};

li.ondblclick = function(){
bottom.removeChild(li);
};
}
.container {
width: 400px;
margin: 50px auto;
text-align: center;
font-family: verdana;
}

#top, #bottom {
width: 300px;
margin: 20px auto;

}

#input {
width: 220px;
padding: 5px 0;
font-size: 16px;
outline: none;
border: 0;
border-bottom: 3px solid #84ac47;

}

#add {
width: 73px;
padding: 9px;
margin:0
outline: none;
background: #67c100;
color: #fff;
border: 0;
}

li {
list-style-type: none;
float: left;
line-height:36px;
padding: 8px 0;
font-size: 15px;
font-weight: 300;
text-align: left;
width: 300px;
margin-left:0;
padding-left:0;
border-bottom: 2px solid #eeeeee;
color: #67c100;


}

img {
float: right;
height: 35px;
width: 35px;
padding-left: 10px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="shoppinglist.css">
<title>Shopping List</title>
</head>
<body>
<div class="container">
<h2>Shopping List</h2>
<div id='top'><!--
--><input type="text" id="input"><!--
--><button id="add">ADD</button>
</div>

<div id='bottom'>
<ul id="list"></ul>
</div>
</div>
<script src="shoppinglist.js"></script>
</body>
</html>

最佳答案

试试这个:

var items = [];
var index = 0;

function createListItem(text, done, dontStore) {
var li = document.createElement('li');
var imageRemove = document.createElement('img');

imageRemove.setAttribute('src', 'http://findicons.com/files/icons/1580/devine_icons_part_2/128/trash_recyclebin_empty_closed.png');

li.textContent = text;
var i = index;
if (!dontStore) {
items[index++] = {text: text, done: false};
localStorage.setItem('items', JSON.stringify(items));
}
li.appendChild(imageRemove);
if (!done) {
var imageDone = document.createElement('img');
imageDone.setAttribute('src', 'https://cdn0.iconfinder.com/data/icons/round-ui-icons/512/tick_green.png');
li.appendChild(imageDone);
imageDone.onclick = function(){
li.removeChild(imageRemove);
items[i].done = true;
localStorage.setItem('items', JSON.stringify(items));
};
}
bottom.appendChild(li);

function remove() {
bottom.removeChild(li);
delete items[i];
localStorage.setItem('items', JSON.stringify(items));
}
imageRemove.onclick = remove;

li.ondblclick = remove;
}

function ShoppingList() {
createListItem(input.value);
input.value = "";
input.focus();
}


items = JSON.parse(localStorage.getItem('items') || '[]');
items.forEach(function(item) {
if (item) {
createListItem(item.text, item.done, true);
}
});

关于javascript - 如何使用javascript在localstorage中存储带有文本和图像的li,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35177913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com