2015-02-05 75 views
0

我想在我的本地机器上使用jquery.cookie插件设置一个cookie。我已经下载了需要的jquery文件,并用此代码创建一个html文件:如何使用插件jquery.cookie?

<!DOCTYPE html> 
<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <script type="text/javascript" src="jquery-1.11.2.min.js "></script> 
    <script type="text/javascript" src="jquery.cookie.js "></script> 
    </head> 
    <body> 
    <script> 
    $(document).ready(function(){ 
     $.cookie('name','value'); 
    }); 
    </script> 
    </body> 
</html> 

但是,它并未在Google Chrome中设置Cookie。哪里不对?谢谢。

回答

0

我想出了如何做到这一点:首先html文件必须在服务器中打开。我已经配置了apache,所以我用localhost打开文件。然后代码:

<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <script type="text/javascript" src="../javaframeworks/jquery-1.11.2.min.js"></script> 
    <script type="text/javascript" src="../javaframeworks/jquery-cookie/src/jquery.cookie.js"></script> 
    <link rel="stylesheet" type="text/css" href="../css/shadowbox.css" /> 
    <script type="text/javascript"> 
     if ($.cookie('backdrop')==null){ 
      $(document).ready(function(){ 
      $('.backdrop, .box').animate({'opacity':' 0.85'}, 250, "linear"); 
      $('.box').animate({'opacity':' 1.00'}, 250, "linear"); 
      $('.backdrop , .box').css('display', 'block'); 
      $('.close').click(function(){ 
       close_box(); 
      }); 
      $('.backdrop').click(function(){ 
       close_box(); 

      }); 

      }); 
      $.cookie('backdrop','1'); 
     } 
     function close_box(){ 
      $('backdrop, .box').animate({'opacity':'0'},250, 'linear',function(){ 
       $('.backdrop , .box').css('display', 'none'); 
      }); 
     } 
    </script> 
</head> 
<body> 
    <div class="backdrop"></div> 
    <div class="box"> 
     <div class="close"><img src="../img/big_close.png"></div> 
     <div class="contentbox"> 
      <img src="../img/warning.jpg" id="aviso"> 
      <h4 id="msg"> Está página está em fase de desenvolvimento</h4> 
     </div> 
    </div> 
</body> 
</html> 

此代码的目标是在页面内容前面显示带有消息的div并放入opact背景。但只有用户第一次打开该页面。