JQuery cookie operation method example tutorial, jquerycookie

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.