<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>简单的cookie封装</title>
</head>
<body>
</body>
<script>
var cookies={
name:this.name,
value:this.value,
day:this.day,
setCookie:function(name,value,day){
var oDate=new Date;
oDate.setDate(oDate.getDate()+day);
document.cookie=name+‘=‘+value+‘;expires=+‘+oDate;
},
getCookie:function(name){
var arr=document.cookie.split(‘; ‘);
for(var i=0;i<arr.length;i++){
var arr2=arr[i].split("=");
if(arr2[0]==name){
return arr2[1];
}
}
return ‘‘;
}
}
cookies.setCookie(‘sex‘,‘男‘,10)
console.log(cookies.getCookie(‘sex‘));
</script>
</html>
(根据智能社的视频修改的)
时间: 2024-11-02 01:59:59