JQuery cookie operation method example tutorial, jquerycookie
This document describes how jQuery operates cookies. Share it with you for your reference. The specific method is as follows:
Let's take a look at the aip of jq. cookie.
Write cookie
Copy codeThe Code is as follows: $. cookie ("this-cookie", "this-value ",{
Expires: 10, // valid date
Path: "/", // cookie path
Domanin: // cookie Domain Name
Secure: true // true. A security protocol is required for cookie transmission. Otherwise, the opposite is required.
});
Read cookie
Copy codeThe Code is as follows: $. cookie ("this-cookie ")
Delete cookie
Copy codeThe Code is as follows: $. cookie ("this-cookie", null)
Is it easy? In this way, you can complete the cookie. The following is a demo example.
Copy codeThe Code is as follows: $ (function (){
$ ("Ul li"). click (function (){
$ ("#" + This. id). addClass ("cur"). siblings (). removeClass ("cur"); // switch the selected Style
$ ("# Colortable"). attr ("href", this. id + ". css"); // change the corresponding style sheet during each switchover
$. Cookie ("cookie", // write cookie
This. id, // business that requires cookie writing
{
"Path": "/", // default cookie attributes
"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:
Copy codeThe Code is as follows: <li id = "colour_1"> Red </li>
<Li id = "colour_2"> black </li>
A simple skin change is coming out.
If you open it in Google's browser, remember to go to the server ..
Note the following points in the demo:
The box to be clicked. The class or id is the same as the corresponding style table name.
This completes pulling.
The Code is as follows:
Copy codeThe 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
})
}
I hope this article will help you with jQuery programming.