1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Document</title>
    8. </head>
    9. <body>
    10. </body>
    11. <script>
    12. var manageCookies = {
    13. set: function (key, value, expTime) {
    14. // console.log(key, value, expTime);
    15. document.cookie = key + '=' + value + ';max-age=' + expTime;
    16. return this;
    17. },
    18. delete: function (key) {
    19. return this.set(key, '', -1)
    20. },
    21. get: function (key, cb) {
    22. // 遍历cookie的所有项
    23. var CookiesArray = document.cookie.split('; ');
    24. console.log(CookiesArray);
    25. for (var i = 0; i < CookiesArray.length; i++) {
    26. var CookieItem = CookiesArray[i];
    27. var CookieItemArray = CookieItem.split('=');
    28. // 遍历拆分每一项的键值
    29. console.log(CookieItemArray);
    30. if (CookieItemArray[0] == key) {
    31. // 如果找到则打印属性值
    32. cb(CookieItemArray[1]);
    33. return this;
    34. }
    35. }
    36. cb(undefined);
    37. return this;
    38. },
    39. }
    40. manageCookies.set('name', 'xiaohu', 1000).set('age', '20', 1000).delete(name).get('age', function (data) {
    41. console.log(data);
    42. });
    43. // console.log(document.cookie);
    44. </script>
    45. </html>