Cookies html + js添加cookie不工作

Cookies html + js adding cookie not working

本文关键字:cookie 工作 添加 js html Cookies      更新时间:2023-09-26

你好,我有一个脚本,应该添加cookie滑动时,按钮被点击。但它根本不起作用……我尝试了所有,但仍然不工作:S

下面是我的代码:
<head>
 <script src="jquery.min.js"></script>
 <script>
  $(document).ready(function(){
   $(".btn1").click(function(){
     $("p").slideUp();
   });
  });
 </script>
 <script>
  function iLoveCookies(){
    days=30; // number of days to keep the cookie
    myDate = new Date();
    myDate.setTime(myDate.getTime()+(days*24*60*60*1000));
    document.cookie = 'cookieName=12345; expires=' + myDate.toGMTString();
  }
 </script>
</head>
<body>
  <p>12345 <button class="btn1" onclick="iLoveCookies()">OK</button></p>

谢谢你的帮助!

了解jquery。饼干:https://github.com/js-cookie/js-cookie。

<script type="text/javascript" src="jquery.cookie.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $.cookie('the_cookie', 'the_value'); // default expires: 365
        $.cookie('the_cookie', 'the_value', { expires: 7 }); // expiring in 7 days 
        console.log($.cookie('the_cookie')); // => "the_value"
    });
</script>

注:你需要通过网络服务器。在浏览器中打开html文件将无法工作