Home > Article > Web Front-end > jQuery+Cookie implements skin switching function
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 () { $('#skin>li').click(function () { var skinID = this.id; switchSkin(skinID); }); var skinID = $.cookie("skinID"); //获取cookie if (skinID) {//如果cookie存在,切换皮肤 switchSkin(skinID); } }); function switchSkin(skinID) { $('#' + skinID).addClass('selected') .siblings().removeClass('selected'); var cssHref = 'Styles/Skins/' + skinID + '.css'; $('#cssfile').attr('href', 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>
Operation effect:
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future. .
Related articles:
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!