跨页面,a->b,b传数组给a

原创 yinglsc 译文 js 230阅读 2018-03-08 15:36:20 举报

实际情形是
course creat(a) 和creat2(b)
a页面点击按钮跳到b页面,b页面类似于提交表单,提交后返回a页面,a页面则多了一条数据用来展示以及删除,然后a页面再点按钮...此过程可以重复
b页面
var obj = new Object();
obj.week = week;
obj.time = time;
obj.classroom=classroom;

if(localStorage.courses==null){
var c = [obj];
//obj为数组,c为数组对象
console.log(c);

//JSON.stringify()【从一个对象中解析出字符串】
//[{"week":"周一","time":"5-6节","classroom":"s"}]
var courses = JSON.stringify(c);
console.log(courses);
localStorage.courses=courses;
}else{
//JSON.parse()【从一个字符串中解析出json对象】
var courses = JSON.parse(localStorage.courses);
courses.push(obj);
console.log(courses);
localStorage.courses=JSON.stringify(courses);
}
history.go(-1);

a页面
window.addEventListener('pageshow', function(e) {
loadCourse();
});

function loadCourse(){
var courses = JSON.parse(localStorage.courses);
console.log(courses);
if(courses!=null){
$("#list").html("");
for(var i=0;i<courses.length;i++){
$("#list").append('<li class="mui-table-view-cell">'
+'<span>'+courses[i].week+' </span>'
+'<span>'+courses[i].time+' </span>'
+'<span>'+courses[i].classroom+'</span>'
+'<i onclick="del('+i+')" class="my_beacondel mui-icon mui-icon-closeempty"></i>'
+'</li>')
}
}
}
function del(id){
var courses = JSON.parse(localStorage.courses);
//从courses的循环传来的第i个开始删一个元素
courses.splice(id,1);
var c = JSON.stringify(courses);
localStorage.courses=c;
loadCourse();
}

评论 ( 1 )
最新评论
lawrence 2018-03-09 02:22:40 1F

通过localStorage搞不叫传参吧