简体   繁体   English

设置 cookie 以在单击按钮时隐藏 div

[英]Set cookie to hide div when button is clicked

I'm trying to display a div (containing terms and conditions) which is shown by default unless a cookie is set to hide it.我正在尝试显示默认情况下显示的 div(包含条款和条件),除非将 cookie 设置为隐藏它。

I have added my code here to a JSFiddle我已将我的代码添加到JSFiddle

Any ideas why my code isn't working?任何想法为什么我的代码不起作用?

JavaScript JavaScript

    function TermsAndConditions(){
   days=30;
   myDate = new Date();
   myDate.setTime(myDate.getTime()+(days*24*60*60*1000));
   document.cookie = 'TermsAndConditions=Accepted; expires=' + myDate.toGMTString();
}
if ($.cookie("TermsAndConditions") !== Accepted)
{
    $("#terms-and-conditions").hide();
} 

HTML HTML

  <h1>Terms and Conditions</h1>
    <p>Example Content</p>

  <span class="accept-button"><a href="#" onclick="TermsAndConditions()">Accept</a></span></div

>

There's a lot going on with your script.你的脚本有很多事情要做。

Mark's got you on the right start: not quoting Accepted is not going to make JavaScript happy; Mark 为您提供了正确的开始:不引用Accepted不会让 JavaScript 高兴; string literals have to have quotes.字符串文字必须有引号。

Secondly, your jsFiddle doesn't link jQuery in. On the left side of the screen, under "Frameworks & Extensions", make sure you've chosen a version of jQuery.其次,您的 jsFiddle 没有链接 jQuery。在屏幕左侧的“框架和扩展”下,确保您选择了一个 jQuery 版本。

Thirdly, jQuery doesn't have a built-in $.cookie method;第三,jQuery 没有内置的$.cookie方法; there are extensions that provide that, but it's not default jQuery.有一些扩展提供了这一点,但它不是默认的 jQuery。 And if you were using a jQuery plugin, wouldn't you use that to set the cookie too?如果您使用的是 jQuery 插件,您是否也不会使用它来设置 cookie? Instead you use native browser extensions to set the cookie.相反,您使用本机浏览器扩展来设置 cookie。

Fourthly, because of the way jsFiddle works, you can't invoke JavaScript methods from within the HTML without some gymnastics.第四,由于 jsFiddle 的工作方式,如果没有一些技巧,您无法从 HTML 中调用 JavaScript 方法。 Much better to attach the appropriate functionality all within the JavaScript.在 JavaScript 中附加适当的功能要好得多。

So, to get your cookie, you can do this:因此,要获取您的 cookie,您可以这样做:

var cookie = document.cookie.split(';')
    .map(function(x){ return x.trim().split('='); })
    .filter(function(x){ return x[0]==='TermsAndConditions'; })
    .pop();

If the cookie exists, it will be a key/value array.如果 cookie 存在,它将是一个键/值数组。 Then you can check to make sure it exists and contains the value you're looking for:然后您可以检查以确保它存在并包含您要查找的值:

if(cookie && cookie[1]==='Accepted') {
    $("#terms-and-conditions").hide();
}

Then we'll move the click functionality out of the HTML and into the JavaScript:然后我们将点击功能从 HTML 中移到 JavaScript 中:

$('.accept-button a').on('click', function(){
    TermsAndConditions();
    return false;
}); 

Here's a working jsFiddle for you: http://jsfiddle.net/3d928/1/这是一个适合您的 jsFiddle: http : //jsfiddle.net/3d928/1/

Note that in your original example, you aren't hiding the #terms-and-conditions element when the user clicks the link;请注意,在您的原始示例中,当用户单击链接时,您并没有隐藏#terms-and-conditions元素; all you're doing is setting the cookie.你所做的就是设置cookie。 So it won't be hidden until the user re-loads the page.所以它不会被隐藏,直到用户重新加载页面。 If you want to do both, simply hide the element in the click handler as well:如果您想同时执行这两项操作,只需将元素隐藏在点击处理程序中:

$('.accept-button a').on('click', function(){
    TermsAndConditions();                   // set cookie
    $("#terms-and-conditions").hide();      // hide element
    return false;
}); 

Check this out : http://jsfiddle.net/bochzchan/fkFSZ/看看这个: http : //jsfiddle.net/bochzchan/fkFSZ/

function TermsAndConditions(){       
   days=30;
   myDate = new Date();
   myDate.setTime(myDate.getTime()+(days*24*60*60*1000));    
   document.cookie = 'TermsAndConditions=Accepted; expires=' + myDate.toGMTString();     
}

function CheckCookies(){   
    if ($.cookie("TermsAndConditions") === "Accepted")
    {
        $("#terms-and-conditions").hide();
    } 
}

Maybe you forget to include jquery cookie js也许你忘记包含 jquery cookie js

<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>
<div id="terms-and-conditions">
    <h1>Terms and Conditions</h1>
    <p>Example Content</p>  
    <span class="accept-button"><a href="#" onclick="TermsAndConditions()">Accept</a></span>    
</div>
<a href="#" onclick="CheckCookies()">Check</a>

There are a couple things to consider:有几件事情需要考虑:

  1. You should have quotes around Accepted in the if statement.您应该在if语句中对Accepted引号。
  2. You should use the === comparison, not !== .您应该使用===比较,而不是!==

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

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