//js操作cookies //设置cookies setCookie("name", "张三") setCookie("sex", "1") setCookie("age", "2") setCookie("calss", "张三") alert(getCookie("sex")) alert(delCookie("sex")) function setCookie(name, value) { var date = new Date(); var expires = 10; date.setTime(date.getTime() + expires * 24 * 60 * 60 * 1000) //添加cookies设置过期时间 document.cookie = name + "=" + value + ";expires=" + date.toGMTString(); } //获取cookies function getCookie(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]; } } } //删除cookis function delCookie(name) { var exp = new Date(); exp.setTime(exp.getTime() - 1); var cval = getCookie(name); if (cval != null) { document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString(); } }