时间:2021-07-01 10:21:17 帮助过:15人阅读
1)关键代码
<!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>w 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>
相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!
推荐阅读:
css的新属性display:box使用方法
node做出个性命令行工具
以上就是jQuery+Cookie切换风格的详细内容,更多请关注Gxl网其它相关文章!