// - setCookie()——创建Cookie
function setCookie(key, val, time) {
var date = new Date();
date.setDate(date.getDate() + time);
document.cookie = `${key}=${val};expires=${date}`;
}
// - getCookie()——获取Cookie
function getCookie(key) {
var arry = document.cookie.split('; ');
for (var item of arry) {
var newArry = item.split('=');
if (newArry[0] == key) {
return newArry[1]
}
}
}
// - removeCookie()——删除Cookie
function removeCookie(key_) {
this.setCookie(key_, '', -1);
}
// 清空cookie
function clearCookie() {
var arry = document.cookie.split('; ');
for (var item of arry) {
var newArry = item.split('=');
console.log(newArry[0]);
this.removeCookie(newArry[0]);
}
}
cookie的封装
最新推荐文章于 2024-11-05 22:07:06 发布