The example in this article describes how jQuery operates cookies. Share it with everyone for your reference. The specific method is as follows:
Let’s take a look at jq.cookie’s aip first
Write cookie
$.cookie("this-cookie","this-value" ,{
expires:10,//Expiration date
Path:"/",//Cookie path
Domanin: //Domain name of cookie
secure:true //true, cookie transmission will require a secure protocol, otherwise vice versa
});
Read cookie
$.cookie("this-cookie")
Delete cookies
$.cookie("this-cookie",null)
Isn’t it very simple? In this way, you can complete the cookie. Let’s see a demo example
$(function(){
$("ul li").click(function(){
$("#" this.id).addClass("cur").siblings().removeClass("cur"); //Switch the selected style
$("#colortable").attr("href",this.id ".css");//Replace the corresponding style sheet each time you switch
$.cookie("cookie",//Write cookie
This.id,//Business that requires cookie writing
{
"path":"/", //Default attributes of cookie
"expires":10 //Valid days
})
});
var cookie=$.cookie("cookie"); //Read cookie
If(cookie){
$("#" cookie).addClass("cur").siblings().removeClass("cur");
$("#colortable").attr("href",cookie ".css");
$.cookie("cookie",cookie,{
"path":"/",
"expires":10
})
}
})
html page:
red
black
A simple skin resurfacing effect is achieved
If you open it with Google Chrome, remember to do it on the server side. .
Things to note about the demo above are:
The clicked box. The class or id is the same as the corresponding style sheet name.
This completes the pull.
The compiled code is as follows:
$(function(){
$("ul li").click(function(){
Mycookie(this.id)
});
var cookie=$.cookie("cookie"); //Read cookie
If(cookie){
Mycookie(cookie);
}
})
function Mycookie(thiscookie){
$("#" thiscookie).addClass("cur").siblings().removeClass("cur");
$("#colortable").attr("href",thiscookie ".css");
$.cookie("cookie",thiscookie,{
"path":"/",
"expires":10
})
}
Readers who are interested in more content related to jquery cookie operation can check out the special topic of this site: "Summary of jQuery's cookie operation skills"
I hope this article will be helpful to everyone’s jQuery programming.