代码语言:javascript复制
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>封装cookie</title>
</head>
<body>
</body>
<script src="./cookie.js"></script>
<script>
const {cookie}= cookies; //引入声明一下
cookie.set('maomin', '22', '0.5');
console.log(cookie.get("maomin"));
</script>
</html>
cookie.js
const cookies = {
cookie: {
// 设置cookie
set: (name, value, day) => {
const date = new Date();
date.setDate(date.getDate() day);
document.cookie = name "=" value ";expires=" date;
},
// 获取cookie
get: (key) => {
var arr = document.cookie.split("; ");
for (var i = 0; i < arr.length; i ) {
var arr1 = arr[i].split("=");
if (arr1[0] == key) {
return arr1[1]
更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/120032703