Javascript toDoList使用本地存储
我必须用本地存储做一个简单的todo应用程序。我已经做了这个应用程序,但没有本地存储。 如何在todoList中添加本地存储?我的意思是,如果我刷新页面,我的todoList会自动保存。我做不到。如果有人能帮助我,我将非常感激Javascript toDoList使用本地存储,javascript,Javascript,我必须用本地存储做一个简单的todo应用程序。我已经做了这个应用程序,但没有本地存储。 如何在todoList中添加本地存储?我的意思是,如果我刷新页面,我的todoList会自动保存。我做不到。如果有人能帮助我,我将非常感激 var todoList = []; function addTodo(frm) { var todo = frm.txtTodo; if(todo.value != "") { todoList.push({"title":tod
var todoList = [];
function addTodo(frm)
{
var todo = frm.txtTodo;
if(todo.value != "")
{
todoList.push({"title":todo.value,"status":1});
todo.value="";
}
else
{
alert("you have to write something!");
return false;
}
todo.focus();
displayTodoItems();
return false;
}
function todoAction(no,action)
{
if(no != null)
{
todoList[no].status = action;
}
displayTodoItems();
}
function todoDelete(no)
{
todoList.splice(no,1);
displayTodoItems();
}
function displayTodoItems(){
var contStart = "<ul>";
var listItems = "";
var contEnd = "</ul>";
if(todoList.length > 0)
{
for(var i=0; i < todoList.length; i++)
{
var actionClass = todoList[i].status==0?"Done":"Not done";
var setStatus = todoList[i].status==0?1:0;
listItems+= "<li class='"+actionClass+"'><span class='title'>"+todoList[i].title+"</span>"
listItems+= "<span class='action'><a href='javascript:void(0);' onClick='todoAction("+i+","+setStatus+");'>";
listItems+= todoList[i].status==0?"Not done":"Done";
listItems+= "</a> <a href='javascript:void(0);' onClick='todoDelete("+i+");'> X </a>";
listItems+="</span></li>";
}
}
else
{
listItems+= "<li><span class='title'>There are not tasks!</span></li>";
}
document.getElementById("todoList").innerHTML = contStart+listItems+contEnd;
}
var-todoList=[];
函数addTodo(frm)
{
var todo=frm.txtodo;
如果(todo.value!=“”)
{
todoList.push({“title”:todo.value,“status”:1});
todo.value=“”;
}
其他的
{
警惕(“你必须写点东西!”);
返回false;
}
todo.focus();
displayTodoItems();
返回false;
}
函数todoAction(否,操作)
{
如果(否!=null)
{
todoList[no]。状态=操作;
}
displayTodoItems();
}
要删除的函数(否)
{
todoList.拼接(编号1);
displayTodoItems();
}
函数displayTodoItems(){
var contStart=“”;
var listItems=“”;
var content=“
”;
如果(todoList.length>0)
{
for(var i=0;i”+todoList[i]。标题+“”
列表项+=“”;
列表项+=“”;
}
}
其他的
{
listItems+=“没有任务! ”;
}
document.getElementById(“todoList”).innerHTML=contStart+listItems+content;
}
使用本地存储创建待办事项列表的简单示例:
由于安全问题,以下代码无法在堆栈溢出中工作。请在您的系统上尝试
(函数(){
var list=document.querySelector(“#list”),
表单=document.querySelector('form'),
item=document.querySelector(“#item”);
表.addEventListener('submit',函数(e){
e、 预防默认值();
list.innerHTML+=''+item.value+' ';
store();
item.value=“”;
},错)
list.addEventListener('click',函数(e){
var t=e.target;
if(t.classList.contains('checked')){
t、 parentNode.removeChild(t);
}否则{
t、 添加('checked');
}
store();
},错)
函数存储(){
window.localStorage.myitems=list.innerHTML;
}
函数getValues(){
var storedValues=window.localStorage.myitems;
如果(!StoredValue){
list.innerHTML='制作一个待办事项列表 '+
“勾选待办事项列表上的第一件事”+
“意识到你已经完成了列表中的两件事”+
‘打个盹儿奖励自己 ’;
}
否则{
list.innerHTML=存储值;
}
}
getValues();
})();代码>
正文{
背景:#eee;
颜色:#444;
字体系列:“Helvetica”,arial,无衬线;
}
.集装箱{
背景:#fff;
最大宽度:600px;
宽度:100%;
显示:表格;
保证金:0自动;
边缘顶端:40px;
边框:1px实心#CFCbc;
}
输入{
边界:无;
显示:块;
宽度:98.4%;
线高:1.5;
填充:8px 0 8px 10px;
边框底部:1px实心#cfcbcc;
大纲:0;
}
h1,
氢{
文本对齐:居中;
边缘底部:0px;
线高:1;
}
氢{
颜色:灰色;
}
保险商实验室{
列表样式:无;
保证金:0;
填充:0;
李{
颜色:#899098;
字体大小:400;
边框底部:1px实心#cfcbcc;
线高:1.5;
填充:8px0;
&:之前{
内容:“\25A1”;
填充:10px 10px;
字体大小:20px;
}
&:悬停{
文字装饰:线条贯通;
光标:指针;
}
&:最后一个孩子{
边框底部:无;
}
}
}
.检查{
颜色:绿色;
&:之前{
内容:“\2713”;
填充:10px 10px;
字体大小:20px;
}
&:悬停{
文字装饰:无;
&:之后{
内容:“(删除)”;
颜色:番茄;
文本对齐:右对齐;
}
}
}
待办事项列表
使用本地存储创建待办事项列表的简单示例:
由于安全问题,以下代码无法在堆栈溢出中工作。请在您的系统上尝试
(函数(){
var list=document.querySelector(“#list”),
表单=document.querySelector('form'),
item=document.querySelector(“#item”);
表.addEventListener('submit',函数(e){
e、 预防默认值();
list.innerHTML+=''+item.value+' ';
store();
item.value=“”;
},错)
list.addEventListener('click',函数(e){
var t=e.target;
if(t.classList.contains('checked')){
t、 parentNode.removeChild(t);
}否则{
t、 添加('checked');
}
store();
},错)
函数存储(){
window.localStorage.myitems=list.innerHTML;
}
函数getValues(){
var storedValues=window.localStorage.myitems;
如果(!StoredValue){
list.innerHTML='制作一个待办事项列表 '+
“勾选待办事项列表上的第一件事”+
“意识到你已经完成了列表中的两件事”+
‘打个盹儿奖励自己 ’;
}
否则{
list.innerHTML=存储值;
}
}
getValues();
})();代码>
正文{
背景:#eee;
颜色:#444;
字体系列:“Helvetica”,arial,无衬线;
}
.集装箱{
背景:#fff;
最大宽度:600px;
宽度:100%;
显示:表格;
保证金:0自动;
边缘顶端:40px;
边框:1px实心#CFCbc;
}
输入{
边界:无;
显示:块;
宽度:98.4%;
线高:1.5;
填充:8px 0 8px 10px;
边框底部:1px实心#cfcbcc;
大纲:0;
}
h1,
氢{
文本对齐:居中;
边缘底部:0px;
线高:1;
}
氢{
颜色:灰色;
}
保险商实验室{
列表样式:无;
保证金:0;
填充:0;
李{
颜色:#899098;
字体大小:400;
边框底部:1px实心#cfcbcc;
线高:1.5;
衬垫: