Home > Web Front-end > JS Tutorial > jQuery+Cookie implements skin switching function

jQuery+Cookie implements skin switching function

亚连
Release: 2018-05-29 09:49:33
Original
1556 people have browsed it

This article mainly introduces jQuery Cookie to realize the skin switching function, and analyzes the related operating techniques of jQuery combined with cookie to dynamically change the page element style in the form of a complete example. It also comes with source code for readers to download for reference. Friends in need can refer to it

The example in this article describes how jQuery Cookie implements the skin switching function. I would like to share it with you for your reference. The details are as follows:

I have recently been learning Jquery and found that Jquery is really very powerful. The function of switching skins can be realized in just a few lines of code.

1) Key code

<!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>
 <title>www.jb51.net jQuery Cookie切换皮肤</title>
 <link id="cssfile" href="Styles/Skins/skin_0.css" rel="external nofollow" rel="stylesheet" type="text/css" />
 <link href="Styles/Site.css" rel="external nofollow" rel="stylesheet" type="text/css" />
 <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
 <script src="Scripts/jquery.cookie.js" type="text/javascript"></script>
 <script language="javascript" type="text/javascript">
  $(function () {
   $(&#39;#skin>li&#39;).click(function () {
    var skinID = this.id;
    switchSkin(skinID);
   });
   var skinID = $.cookie("skinID"); //获取cookie
   if (skinID) {//如果cookie存在,切换皮肤
    switchSkin(skinID);
   }
  });
  function switchSkin(skinID) {
   $(&#39;#&#39; + skinID).addClass(&#39;selected&#39;)
       .siblings().removeClass(&#39;selected&#39;);
   var cssHref = &#39;Styles/Skins/&#39; + skinID + &#39;.css&#39;;
   $(&#39;#cssfile&#39;).attr(&#39;href&#39;, cssHref);
   $.cookie("skinID", skinID, { path: "/", expires: 10 }); //将皮肤样式的id保存到cookie中
  }
 </script>
</head>
<body>
 <form id="form1" runat="server">
 <p id="header">
  hello world!
  <ul id="skin">
   <li id="skin_0" title="蓝色" class="selected">蓝色</li>
   <li id="skin_1" title="紫色">紫色</li>
   <li id="skin_2" title="红色">红色</li>
   <li id="skin_3" title="天蓝色">天蓝色</li>
   <li id="skin_4" title="橙色">橙色</li>
   <li id="skin_5" title="淡绿色">淡绿色</li>
  </ul>
 </p>
 </form>
</body>
</html>
Copy after login

Operation effect:

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future. .

Related articles:

jQuery implements the function of jumping between browsers and passing parameters [supports Chinese characters]

vue page loading Solution to the flickering problem

A brief discussion on the problem of js obtaining the ModelAndView value

The above is the detailed content of jQuery+Cookie implements skin switching function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template