A页面
html代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
姓名:<input type="text" id="name1"> 年龄:<input type="text" id="age1"> <button id="click_btn">点击</button>
js代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
// localStorage.name = "张三"; // localStorage.age = 18;//可存储多组数据 var btn = document.getElementById("click_btn"); btn.onclick = function () { // var username = 'zhangsan' var username = document.getElementById("name1").value; // var userage = '13'; var userage = document.getElementById("age1").value; //存放所有值到数组里 var arrLocalStorage = [username, userage]; //存储,IE6~7 cookie 其他浏览器HTML5本地存储 if (window.localStorage) { localStorage.setItem("localName", arrLocalStorage); } else { Cookie.write("localName", arrLocalStorage); } } //参考了 https://blog.csdn.net/ZhushiKezhang/article/details/80360998
B页面
js代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
// var myname = localStorage["name"];//myname 为张三 // console.log(myname) //使用localStorage 和 Cookie相结合的方式传递参数 //条件判断 取localStorage存储值或cookie存储的值 var StoreDate = window.localStorage ? localStorage.getItem("localName") : Cookie.read("localName"); var arrStoreDate = StoreDate.split(","); console.log(arrStoreDate);//取出所有存储的值 console.log(arrStoreDate[0]);//取出第一个值 姓名 console.log(arrStoreDate[1]);//取出第二个值 年龄 // for (var i = 0; i < arrStoreDate.length; i++) { // var liHtml = "<li>" + arrStoreDate[i] + "</li>"; // $('.list').append(liHtml); // }