var cookie = { // 设置cookie方法 set:function(key, val, time){ // 获取当前时间 var date = new Date(); // 将date设置为n天以后的时间 var expiresDays = time; // 格式化为cookie识别的时间timestamp date.setTime(date.getTime() + expiresDays * 24 * 3600 * 1000); document.cookie = key + "=" + val + ";expires=" + date.toGMTString(); }, // 获取cookie方法 get:function(key){ var getCookie = document.cookie.replace(/[ ]/g, ""); var arrCookie = getCookie.split(";"); var value; for(var i = 0; i < arrCookie.length; i++){ var arr = arrCookie[i].split("="); if(key == arr[0]){ value = arr[1]; break; } } return value; }, // 删除cookie方法 delete:function(key){ // 获取当前时间 var date = new Date(); // 将date设置为过去的时间 date.setTime(date.getTime() - 10000); document.cookie = key + "=v; expires =" + date.toGMTString(); }};// testcookie.set('hello', 'world', 7);var val = cookie.get('hello');console.log(val); // worldcookie.delete('hello');