How to realize skin change function based on cookie in jQuery
This article mainly introduces "how to achieve skin change function based on cookie in jQuery". In daily operation, I believe that many people have doubts about how to achieve skin change function based on cookie in jQuery. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to achieve skin change function based on cookie in jQuery". Next, please follow the editor to study!
Code:
The use of cookie. 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 } $(function () {var cookieClass=getCookie ('class'); / / read the local Cookie if (cookieClass) {$("body") .attr ("class", cookieClass); / / restore the background of the page to the color saved by Cookie} else {$("body") .attr ("class", "fu1") } $(".huanFu ul li") .on ("click", function () {$(this) .addClass ("select"). Siblings () .removeClass ("select"); / / Mark the selected style var fuName=$ (this) .attr ("fuName"); / / get the class name. Explanation: a fuName attribute is created and fu1 is stored in it, but now it is only $("body"). Attr ("class", fuName); / / change the class attribute of body to achieve the effect of background color change function SetCookie (name,value,day) {/ / three values passed, name, value, number of days saved var exp = new Date () / / get the current local time (including date) exp.setTime (exp.getTime () + day*24*60*60*1000); / / save the number of days in milliseconds [xss_clean] = name + "=" + escape (value) + "; expires=" + exp.toGMTString (); / / with name=fu1 Expires=Date {Thu Jun 26 2014 23:23:25 GMT+0800} this long string is saved to the local cookie} SetCookie ("class", fuName,7); / / sets the Cookie procedure}); function getCookie (name) {/ / reads the local Cookie procedure var nameTit=name+ "="; / / the name value at this time is "class", nameTit= "class=" var ca= [XSS _ clean] .split (';'); / / read the content of the local cookie is "xxx.xxx" Xxx.xxx ", so when we remove the';', it will be stored in the ca as an array. For (var iTuno Bandi)