赞
踩
JS的底层j没有封装好Cookie,所以我们使用Cookie导致极不方便,我们自己动手封装一下Cookie,使用的时候引入文件把Cookie调用一下就可以使用了,极大的提高了开发效率。
1:setCookie("键","值","有效时间"):设置Cookie;
2:getCookie("键") :获取Cookie 具有返回值;
3:removeCookie("键"):删除Cookie;
4:clearCookie( ):清空Cookie;
- // 创建Cookie
- // setCookie("键","值","有效时间")
- function setCookie(key, value, time) {
- var now = new Date();
- now.setDate(now.getDate() + time);
- document.cookie = `${key} = ${value}; expires = ${now}`;
- }
-
- // 获取Cookie
- // getCookie("键")
- function getCookie(key) {
- var str = document.cookie;
- var nweArray1 = str.split("; ");
- for (var item of nweArray1) {
- var newArray2 = item.split("=");
- if (newArray2[0] == key) {
- // console.log(newArray2[1]);
- return newArray2[1];
- }
- }
- }
-
- // 删除Cookie
- // removeCoolie("键")
- function removeCookie(key) {
- this.setCookie(key, "", -1);
- }
-
- // 清空Cookie
- // clearCookie()
- function clearCookie() {
- var str = document.cookie;
- var newArray1 = str.split("; ");
- for (var item of newArray1) {
- var newArray2 = item.split("=");
- this.setCookie(newArray2[0], "", -1);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。