首页 > 其他分享 >jquery-todolist删除数据1-66

jquery-todolist删除数据1-66

时间:2023-02-10 10:03:01浏览次数:54  
标签:jquery function console log todolist 66 var data


jquery-todolist删除数据1-66_本地存储

 

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>todoList待办事项</title>
<script src="./js/jquery.min.js"></script>
</head>

<body>
<header>
<section>
<label for="title">ToDoList</label>
<input type="text" id="title" placeholder="添加ToDo">
</section>
</header>
<section>
<h2>正在进行<span id="rodocount"></span></h2>
<ol id="todolist" class="demo-box">

</ol>
<h2>已经完成<span id="rodocount"></span></h2>
<ul id="donelist">

</ul>
</section>
<footer>
Copyright ©2014 todolist.cn
</footer>
<script>
var todolist = [{
title: '我今天吃了八个馒头',
done: false
}, {
title: '我今天学习jq',
done: false
}, ];
localStorage.setItem("todo", JSON.stringify(todolist));
//本地的数组转换为json.stringfy();
var data = localStorage.getItem("todo");
console.log(typeof data);
//获取本地存储数据需要把字符串数据转换为对象形式json.parse
data = JSON.parse(data);
console.log(data);
$(function() {
//todolist渲染到界面
load();
//按下回车 把完整数据存储到本地存储里面
$("#title").on("keydown", function(event) {
if (event.keyCode === 13) {
//先读取本地存储原来的数据
var local = getDate();
console.log(local);
//把local数组更新元素
local.push({
title: $(this).val(),
done: false
});
//把这个数组给本地存储
saveDate(local);
//todolist渲染到界面
load();
}
});
//todolist删除事件
$("ol").on("click", "a", function() {
//获取本地存储
var data = getDate();
console.log(data);
//修改数据
var index = $(this).attr("id");
console.log(index);
});
//读取本地存储的数据
function getDate() {
var data = localStorage.getItem("todolist");
if (data !== null) {
//本地存储里面的数据是字符串格式的
return JSON.parse(data);
} else {
//返回一个数组
return [];
}
}
//保存数据
function saveDate(data) {
localStorage.setItem("todolist", JSON.stringify(data));
}
//渲染数据
function load() {
//读取本地的数据
var data = getDate(data);
console.log(data);
//遍历之前清空
$("ol").empty();
//遍历整个数据
$.each(data, function(i, n) {
console.log(n);
$("ol").prepend("<li><input type='checkbox'><p>" + n.title +
"</p><a href='javascript:;'></a></li> id=" + i + "");


})
}
});
</script>
</body>

</html>

标签:jquery,function,console,log,todolist,66,var,data
From: https://blog.51cto.com/u_15460007/6048621

相关文章

  • jquery-本地存储实现数据录入
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>todoList待......
  • jquery-todolist本地存储加载到页面
     <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>todoList......
  • jquery-回车把数据存储到本地存储里面-63
     <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>todoList......
  • jquery位置3-60
     <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>Document<......
  • jquery位置2-59
     <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>Document<......
  • jquery位置1
      <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>Documen......
  • jquery-全选按钮下-30
     <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>Document<......
  • jquery尺寸 位置大小-57
     <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>Document<......
  • POJ--3669 Meteor Shower(bfs/稍微变通)
    记录21:372023-2-9http://poj.org/problem?id=reference:《挑战程序设计竞赛(第2版)》第二章练习题索引p135DescriptionBessiehearsthatanextraordinarymeteor......
  • JQuery 入门 - 附案例代码
    文章目录​​预备知识与后续知识及项目案例​​​​为什么要学jquery​​​​体验jquery的使用​​​​jquery到底是什么​​​​jquery的版本问题​​​​jquery的入口函......