zoukankan      html  css  js  c++  java
  • JQuery Cookie使用

    JQuery Cookie插件下载 https://github.com/js-cookie/js-cookie

    Create session cookie:

    $.cookie('name', 'value');

    Create expiring cookie, 7 days from then:

    $.cookie('name', 'value', { expires: 7 });

    Create expiring cookie, valid across entire site:

    $.cookie('name', 'value', { expires: 7, path: '/' });

    Read cookie:

    $.cookie('name'); // => "value"
    $.cookie('nothing'); // => undefined

    Read all available cookies:

    $.cookie(); // => { "name": "value" }

    Delete cookie:

    // Returns true when cookie was successfully deleted, otherwise false
    $.removeCookie('name'); // => true
    $.removeCookie('nothing'); // => false
    
    // Need to use the same attributes (path, domain) as what the cookie was written with
    $.cookie('name', 'value', { path: '/' });
    // This won't work!
    $.removeCookie('name'); // => false
    // This will work!
    $.removeCookie('name', { path: '/' }); // => true
    $.cookie('name', '');

    判断Cookie是否存在

    if ($.cookie("name") == "value") {
        console.log('Cookie已经存在');
    } else {
        console.log("Cookie不存在");
    }

     检测Cookie的例子,如果存在Cookie按钮为灰不可用,如果不存在则正常使用

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <link rel="stylesheet" href="css/bootstrap.css">
        <script src="js/jquery-3.3.1.js"></script>
        <script src="js/jquery.cookie.js"></script>
        <title>Document</title>
    </head>
    
    <body style="padding-top:30px">
        <div class="container">
            <input type="text" name="" id="user">
            <div id='user'>
                <a href="echo.php?id=10" class="btn btn-danger btn-sm">提交</a>
                <a href="echo.php?id=11" class="btn btn-danger btn-sm">提交</a>
                <a href="echo.php?id=12" class="btn btn-danger btn-sm">提交</a>
                <a href="echo.php?id=13" class="btn btn-danger btn-sm">提交</a>
            </div>
            <a href="echo.php?id=14" class="btn btn-danger btn-sm">提交</a>
            <a href="echo.php?id=15" class="btn btn-danger btn-sm">提交</a>
            <a href="echo.php?id=16" class="btn btn-danger btn-sm">提交</a>
            <a href="echo.php?id=17" class="btn btn-danger btn-sm">提交</a>
            <a href="echo.php?id=18" class="btn btn-danger btn-sm">提交</a>
            <a href="echo.php?id=19" class="btn btn-danger btn-sm">提交</a>
        </div>
        <script>
            $(function () {
                if($.cookie("name")=="value"){
                    $('#user a').removeClass().addClass("btn btn-default btn-sm disabled");
                    console.log('Cookie已经存在');
                }else{
                    console.log("Cookie不存在");
                }
    
                $("a").click(function (event) {
                    event.preventDefault();
                    $('#user a').removeClass().addClass("btn btn-default btn-sm disabled");
                    $.cookie("name","value");
                    });
                });
            })
        </script>
    </body>
    </html>
  • 相关阅读:
    [leetcode]34.Find First and Last Position of Element in Sorted Array找区间
    [leetcode]278. First Bad Version首个坏版本
    [leetcode]367. Valid Perfect Square验证完全平方数
    [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)
    [leetcode]55. Jump Game青蛙跳(能否跳到终点)
    [leetcode]26. Remove Duplicates from Sorted Array有序数组去重(单个元素只出现一次)
    [leetcode]27. Remove Element删除元素
    [leetcode]20. Valid Parentheses有效括号序列
    [leetcode]15. 3Sum三数之和
    C#中的局部类型
  • 原文地址:https://www.cnblogs.com/liessay/p/8406870.html
Copyright © 2011-2022 走看看