更換網頁背景(寫入cookie)

來源:互聯網
上載者:User

標籤:har   one   綠色   tom   css   w3c   mmu   ber   儲存   

根據cookie儲存使用者操作狀態

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>  <title>更換網頁背景的jquery代碼(寫入cookie)</title>  <style type="text/css">    body, p, ul {  margin: 0;  padding: 0;  }    ul {float: right;height: 40px;margin-top: 20px;margin-right: 20px;}    ul li {list-style-type: none;float: left;width: 20px;height: 20px;margin-left: 10px;cursor: pointer;}    .skin {height: 40px;position: fixed;background: #fff;border-bottom: solid 1px #ccc;top: 0;left: 0;width: 100%;}    .red {background: #f9bdbe;}    /*紅色*/    .black {background: #000;}    /*黑色*/    .blue {background: #b9daf9;}    /*藍色*/    .green {background: #b7e5bd;}    /*綠色*/  </style>  <script type="text/javascript" src="../jquery-1.11.2.js"></script>  <script type="text/javascript">    $(document).ready(function () {      //為了安全 google chrome 等瀏覽器是禁止本地檔案寫Cookie的即file:///F:/Lord%20community/lrtk/Untitled-2.html這樣的以file開頭的是不能寫本地檔案的      var cookieClass = getCookie(‘class‘);//讀取需要緩衝的對象。      $("body").attr("class", cookieClass);//      $(".skin_list li").each(function () {        $(this).click(function () {          var className = $(this).attr("class");//儲存當前選擇的類名          $("body").attr("class", className, 30);//把選中的類名給body          function SetCookie(name, value, day)//兩個參數,一個是cookie的名子,一個是值          {            var exp = new Date();    //new Date("December 31, 9998");            exp.setTime(exp.getTime() + day * 24 * 60 * 60 * 1000);            document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();          }          SetCookie("class", className, 30);        })      });    });    function getCookie(name)//取cookies函數    {      var nameEQ = name + "=";      var ca = document.cookie.split(‘;‘);      for (var i = 0; i < ca.length; i++) {        var c = ca[i];        while (c.charAt(0) == ‘ ‘) c = c.substring(1, c.length);        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);      }      return null;    }  </script></head><body><div class="skin">  <ul class="skin_list">    <li style="width:100px;text-align:right;">更換背景:</li>    <li class="red"></li>    <li class="black"></li>    <li class="blue"></li>    <li class="green"></li>  </ul></div></body></html>

 

更換網頁背景(寫入cookie)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.