gpt4 book ai didi

javascript - html5存储在页面重新加载时不显示数据

转载 作者:行者123 更新时间:2023-12-03 01:41:31 25 4
gpt4 key购买 nike

我编写了一个待办事项应用程序,将待办事项数据存储在 html5 存储中。当我刷新页面时,存储的数据未显示。现场直播here 。请在下面找到代码,

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="style.css" type="text/css" rel="stylesheet">
<title>Todo</title>
</head>
<body>
<div class="container">
<div class="header">
<h1>Todo</h1>
<div class="add">
<div class="text">
<input class="data" placeholder="Enter data">

</input>
</div>
<div class="submit">
<div class="submit-text">
Add
</div>
</div>
</div>
</div>
</div>
<script
src="https://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>
<script src="script.js" ></script>
</body>
</html>

脚本.js:

console.log("loads");
$(document).ready(function(){
var count=JSON.parse(localStorage.getItem(todo)).length;
$(".submit").click(function(){
count++;
localStorage.setItem("todo", JSON.stringify($('.data').val()));
var i;
for (i = 0; i < count.length; i++) {
var value = "<div class='todorow'><div class='todoitem'>"+localStorage.getItem("todo").key(i)+"</div><div class='todoremove'>X</div></div>";
$(".container").append(value);
}
});
$(".container").on("click", ".todoitem", function(){
jQuery(this).addClass("strike");
});
$(".container").on("click", ".todoremove", function(){
jQuery(this).closest('.todorow').addClass("hide");
});
var i;
for (i = 0; i < count.length; i++) {
var value = "<div class='todorow'><div class='todoitem'>"+JSON.parse(localStorage.getItem("todo").key(i))+"</div><div class='todoremove'>X</div></div>";
$(".container").append(value);
}
});

样式.css:

body{
margin:0;
padding:0;
}
.container{
width:600px;
margin:0 auto;
color:#000;
}
.header{
background:#dddddd;
height:200px;
}
.header h1{
color:#FFF;
font-size:50px;
text-align: center;
margin-top:0px;
}
.add{
height:50px;
background:#FFF;
border-left:1px solid #dddddd;
}
.text,.submit{
display:inline-block;
float:left;
}
.text{
width:70%;
height:100%;
}
.submit{
width:30%;
background:green;
height:100%;
font-size: 30px;
}
.data{
font-size: 30px;
/* margin-top: 10px; */
/* padding: 10px 0; */
padding-left: 10px;
width:100%;
border: none;
padding-top: 8px;
padding-bottom: 8px;
}
.submit-text{
/* margin-top: 10px; */
padding: 10px 0;
text-align: center;
color:#FFF;
}
.todorow{
width:100%;
height:50px;
font-size: 30px;
padding-top:10px;
background:#000000;
color:#FFF;
}
.todoitem{
width:95%;
float:left;
display:inline-block;
}
.todoremove{
width:5%;
float:left;
display:inline-block;
}
.strike{
text-decoration: line-through;
}
.hide{
display:none;
}

最佳答案

尝试将待办事项用引号引起来

var count=JSON.parse(localStorage.getItem("todo")).length;

关于javascript - html5存储在页面重新加载时不显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50825105/

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