Home > Web Front-end > JS Tutorial > jQuery cookie operation method example tutorial_jquery

jQuery cookie operation method example tutorial_jquery

WBOY
Release: 2016-05-16 16:30:30
Original
1601 people have browsed it

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

Copy code The code is as follows:
$.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

Copy code The code is as follows:
$.cookie("this-cookie")

Delete cookies

Copy code The code is as follows:
$.cookie("this-cookie",null)

Isn’t it very simple? In this way, you can complete the cookie. Let’s see a demo example

Copy code The 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");//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:

Copy code The code is as follows:
  • 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:

    Copy code The 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.

    Related labels:
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template