核心提示:var saveLocalKey = stuentList;//创建HTML标签 function createHtml() {//拿本地数据var list = read();var allhtml...
var saveLocalKey = "stuentList"; //创建HTML标签 function createHtml() { //拿本地数据 var list = read(); var allhtml = ""; for (var i in list) { var student = list[i]; var city = student.Name; var cityid = student.Nameid; allhtml+='
{{citys}}
'; allhtml=allhtml.replace("{{citys}}",city); allhtml=allhtml.replace("{{id}}",cityid); } $("#cityback").html(allhtml); } function saveLocal(title,id) { //先取出原有的数据 var list = read(); //添加到末尾 for (var i in list) { var students = list[i]; if (students.Name == title) { return false; } } var student = { Name: title, Nameid: id }; list.push(student); //写回去 save(list); } //存数据 function save(list) { if (list.length == 0) { localStorage.removeItem(saveLocalKey); } var listStr = JSON.stringify(list); localStorage.setItem(saveLocalKey, listStr); } //读数据 function read() { var listStr = localStorage.getItem(saveLocalKey); var list; if (listStr == null) { list = []; } else { list = JSON.parse(listStr); } return list; }createHtml() ,saveLocal(title,id);这是封装好的,修改一下参数和html就ok了