1.localStorage - 没有时间限制的数据存储 var arr=[1,2,3]; localStorage.setItem("stor",arr); console.log(localStorage.getItem("stor")); 2.清空localStorage localStorage.clear(); // 3.删除键值对 localStorage.removeItem("stor"); 注意:存入的数据只能以字符串形式存入。 //JSON对象转JSON字符串 var obj = {"a": 1,"b": 2}; obj = JSON.stringify(obj); //转化为JSON字符串 localStorage.setItem("stor1", obj); //JSON字符串转JSON对象 obj=JSON.parse(localStorage.getItem("stor1"));
例如 6个元素 每次刷新页面依次出现一个(不重复出现),最后一个元素出现后从新开始:
<script> // JavaScript Document tips = new Array(6); tips[0] = 'K6661112228'; tips[1] = 'sy66688815'; tips[2] = 'y6668887788'; tips[3] = 'xzc17051001008'; tips[4] = 'Yn6661118882255'; tips[5] = '258258005858'; var i=localStorage.getItem("index"); if(i==null) { index=0; localStorage.setItem("index", "0") }else { var i=localStorage.getItem("index"); index=parseInt(i)+1; if(index>5) { index=0; localStorage.setItem("index", "0") }else{ localStorage.setItem("index", index) } } </script>