zoukankan      html  css  js  c++  java
  • web存储

    <!DOCTYPE html>
    <html>
    
    	<head>
    		<meta charset="UTF-8">
    		<title>Lik - web存储</title>	
    	</head>
    
    	<body>
    		<script type="text/javascript">
    			window.onload = function() {
    
    				//定义一个数组
    				var arr = [];
    				for(var i = 0; i <= 5; i++) {
    					arr[i] = i + "abc";
    				}
    
    				//定义一个对象
    				var a = {
    					a1: 123,
    					a2: 345,
    					a3: 456
    				}
    				a = JSON.stringify(a); //JSON对象提供的parse和stringify将其他数据类型转化成字符串 
    
    				//localStorage		数据将一直存放在手机缓存中,直到用户清理缓存
    				//sessionStorage 	页面关闭时,销毁存储的数据
    
    				localStorage.a = "dddd"; //存储一个字符串
    				localStorage.b = 55; //存储一个数字
    				localStorage.c = arr; //存储一个数组
    				localStorage.d = a; //存储一个对象	
    				localStorage.f = false; //存储一个布尔值
    				//所有数据都只能被当作字符串存储,不论之前存储的是什么格式,都会被转换为字符串
    
    				var tex1 = localStorage.a;
    				var tex2 = localStorage.b;
    				var tex3 = localStorage.c;
    				var tex4 = JSON.parse(localStorage.d); //将字符串重新转换为对象 				
    				var tex5 = localStorage.f;
    
    				console.log(tex1); //显示字符串
    				console.log(tex2); //显示一个数字字符串
    				console.log(tex3); //显示一个数组字符串
    				console.log(tex4.a2); //显示一个对象,因为在上面一步中,对字符串进行了格式转换
    				console.log(tex5); //显示一个布尔值字符串
    			}
    		</script>
    	</body>
    
    </html>
    

      效果如下图:

    author:Lik
    Endeavoring to powerless, struggling to move yourself.
  • 相关阅读:
    zabbix验证微信
    free
    有名管道和无名管道
    shell实现并发控制
    TCP/IP协议簇 端口 三次握手 四次挥手 11种状态集
    自动化运维
    JSON对象(自定义对象)
    对象中属性的遍历、删除与成员方法
    对象间的赋值操作
    自定义类
  • 原文地址:https://www.cnblogs.com/likwin/p/7134514.html
Copyright © 2011-2022 走看看