Home Web Front-end JS Tutorial jQuery implementation of skin-changing function based on cookies

jQuery implementation of skin-changing function based on cookies

Jan 12, 2018 am 09:56 AM
cookie ie jquery

This article mainly introduces jQuery's skin-changing function based on cookies. It analyzes the operation method of jQuery using cookies to record and read user information to implement page styles based on specific examples. Friends who are interested in jQuery can refer to this article. Article

The example in this article describes how jQuery implements the skin-changing function based on cookies. Share it with everyone for your reference, the details are as follows:

Skin change, you can always see these two words (also called skin) when you use QQ, browser, Kugou and other software. However, skin changing can indeed solve the tastes of many people. Skin changing may seem like an insignificant function, but it can actually attract users. Okay, without further ado, let’s start class.

Attach my code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>cookie的使用</title>

<script type="text/javascript" src="js/jquery-1.8.3.js"></script>

<style>

.huanFu{

  float:right;

}

.huanFu ul li{

  width:30px;height:30px;

  list-style:none;

  margin:0 5px;

  float:left;

  cursor:pointer;

  border:1px solid #000;

}

.fu1{background-color:#F00;}

.fu2{background-color:#0F0;}

.fu3{background-color:#00F;}

.fu4{background-color:#FF0;}

.huanFu ul li.select{border:3px solid #000;margin-top:-3px;}

</style>

<script>

$(function(){

  var cookieClass=getCookie(&#39;class&#39;);//读取本地的Cookie

  if(cookieClass){

    $("body").attr("class",cookieClass);//把页面的背景恢复成Cookie保存的颜色

  }else{

    $("body").attr("class","fu1");

  }

  $(".huanFu ul li").on("click",function(){

    $(this).addClass("select").siblings().removeClass("select");//标示出选中的样式

    var fuName=$(this).attr("fuName");//取得class名。讲解:起了一个fuName属性,在里面存了fu1,现在取出来而已

    $("body").attr("class",fuName);//改变body的class属性来达到背景换色的效果

    function SetCookie(name,value,day){//三个传值,名字、值、保存天数

      var exp = new Date();//取得本机当前时间(含日期)

      exp.setTime(exp.getTime() + day*24*60*60*1000);//把天数变成毫秒保存起来

      document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();//以name=fu1;expires=Date {Thu Jun 26 2014 23:23:25 GMT+0800}这一长串的字符串保存到本机的cookie中

    }

    SetCookie("class",fuName,7);//设置Cookie过程

  });

  function getCookie(name){//读取本地的Cookie过程

    var nameTit=name+"=";//此时的name值就是"class",nameTit="class="

    var ca=document.cookie.split(&#39;;&#39;);//读取本地cookie的内容是"xxx.xxx;xxx.xxx",所以我们去掉&#39;;&#39;后,它会以数组的形式保存入ca内。

    for(var i=0;i<ca.length;i++){//循环ca数组

      var c=ca[i];

      while(c.charAt(0)==&#39; &#39;){//如果开头第一个字符是空格的话,读取就从第二位到最后一位

        c=c.substring(1,c.length);

      }

      if(c.indexOf(nameTit)==0){//判断是否存在,并是否第一位开始的"class="

        return c.substring(nameTit.length,c.length);//取得class=fu1中的"fu1"

      }

      return null;

    }

  }

});

</script>

</head>

<body class="fu1">

  <p class="huanFu">

    <ul>

      <li class="fu1" fuName="fu1"></li>

      <li class="fu2" fuName="fu2"></li>

      <li class="fu3" fuName="fu3"></li>

      <li class="fu4" fuName="fu4"></li>

    </ul>

  </p>

</body>

</html>

Copy after login

After understanding the above code, copy it to your editing software to see the effect. Click the color block in the upper right corner, and the background color of the page changes to the color corresponding to the color block. Then close your browser and open the page again. Are you surprised to find that the color is the color you last closed the browser. I have added corresponding comments in the code. Although the script code is long, it is actually very easy to understand. It uses the browser's cookie to save your value and record your color selection at all times.

Related recommendations:

jquery and ajax realize three-level linkage encapsulation and non-encapsulation of provinces and cities

Ajax cross-domain Solution to the problem of lost access cookies

Detailed explanation of JavaScript to obtain cookies and delete cookies

The above is the detailed content of jQuery implementation of skin-changing function based on cookies. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of jQuery reference methods: Quick start guide Detailed explanation of jQuery reference methods: Quick start guide Feb 27, 2024 pm 06:45 PM

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

How to remove the height attribute of an element with jQuery? How to remove the height attribute of an element with jQuery? Feb 28, 2024 am 08:39 AM

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: &lt

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ​​the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to cancel the automatic jump to Edge when opening IE in Win10_Solution to the automatic jump of IE browser page How to cancel the automatic jump to Edge when opening IE in Win10_Solution to the automatic jump of IE browser page Mar 20, 2024 pm 09:21 PM

Recently, many win10 users have found that their IE browser always automatically jumps to the edge browser when using computer browsers. So how to turn off the automatic jump to edge when opening IE in win10? Let this site carefully introduce to users how to automatically jump to edge and close when opening IE in win10. 1. We log in to the edge browser, click... in the upper right corner, and look for the drop-down settings option. 2. After we enter the settings, click Default Browser in the left column. 3. Finally, in the compatibility, we check the box to not allow the website to be reloaded in IE mode and restart the IE browser.

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

Understand the role and application scenarios of eq in jQuery Understand the role and application scenarios of eq in jQuery Feb 28, 2024 pm 01:15 PM

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

See all articles