<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button onclick="setCookie('house','汤臣一品',2)">设置house</button>
<button onclick="setCookie('car','宝马X7',4)">设置car</button>
<button onclick="getCookie('house')">获取house的值</button>
<button onclick="getCookie('car')">获取car的值</button>
<button onclick="delCookie('house')">删除house的值</button>
<button onclick="delCookie('car')">删除car的值</button>
<script>
function setCookie(name,value,time){
let oDate=new Date()
oDate.setDate( oDate.getDate()+time )
document.cookie=name+'='+value+';expires='+oDate;
}
setCookie('car','X7',2)
function getCookie(name){
let str=document.cookie
// console.log(str);
let arr1=str.split(';')
// console.log(arr1);
for(var i=0;i<arr1.length;i++){
// console.log(arr1[i]);
var arr2=arr1[i].split('=')
console.log(arr2);
if(name==arr2[0].trim()){
console.log(arr2[1]);
}
}
}
function delCookie(name){
let oDate=new Date()
oDate.setDate( oDate.getDate()-1 )
document.cookie=name+'=;expires='+oDate;
}
</script>
</body>
</html>