简体   繁体   English

单击按钮时显示/隐藏 div 并保存 Cookie

[英]Show/Hide div on button click and save Cookies

I use This javascript codes for cookies and show/hide;我将这个 javascript 代码用于 cookie 和显示/隐藏;

$(document).ready(function () {

        if (Boolean(readCookie('hide'))) {
            $('.pop-up').hide();
            $('.pop-up').fadeOut(1000);
        }
        $('.close-button').click(function (e) {

            $('.pop-up').fadeOut(700);
            $('#overlay').removeClass('blur-in');
            $('#overlay').addClass('blur-out');
            e.stopPropagation();

            createCookie('hide', true, 1)
            return false;
        });

        function createCookie(name, value, days) {
            if (days == 1) {
                var date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                var expires = "; expires=" + date.toGMTString();
            }
            else var expires2 = "";
            document.cookie = name + "=" + value + "; expires=" + expires + "; path=/";

          //  $.cookie(name, value, { expires: days });
        }

        function readCookie(name) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') c = c.substring(1, c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
            }
            return null;
        }

        function eraseCookie(name) {
            createCookie(name, "", -1);
        }

    });

I need 10 buttons on same page but I can't.我需要在同一页面上有 10 个按钮,但我不能。 Because if I click close-button (X) all buttons hides.因为如果我单击关闭按钮(X),所有按钮都会隐藏。 I need one by one hide.我需要一张一张的藏起来。

I try to change name and value but unsuccessful.我尝试更改名称和值但未成功。

This is HTML;这是 HTML;

<div id="overlay" class="cover blur-in">
    <div class="content"></div>
</div>
<div class="row pop-up">
    <div class="box small-6 large-centered">
        <a href="#" class="close-button">✖</a>
        <h3 class="popup">Welcome</h3>
        <p>info </p>
        <p>info</p>
        <a href="#" class="button">press for facebook</a>
    </div>
</div>

This is jsfiddle for this code这是这段代码的jsfiddle

Your jsfiddle doesn't seem to be working (blank), but the general idea would be to loop through all elements with close_button class and to add a listener to each, something like this:您的 jsfiddle 似乎不起作用(空白),但总体思路是使用close_button类循环遍历所有元素并为每个元素添加一个侦听器,如下所示:

$('.close-button').each(function(i, obj) {
     $(this).click(function (e) {
         $('.pop-up').eq(i).fadeOut(700);
         $('#overlay').removeClass('blur-in');
         $('#overlay').addClass('blur-out');
         e.stopPropagation();
         return false;
     })
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM