jQuery 检查cookie是否存在并隐藏内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12045918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:14:08  来源:igfitidea点击:

Check if cookie exists and hide content

jquerycsscookies

提问by Hymanson5

EDIT:this works (gives me an alert):

编辑:这有效(给我一个警报):

if (document.cookie.indexOf("TR_LNG") >= 0) { alert('cookie found'); }

So it's the hiding mechanism thats the problem. BUT in console: $('.langselmain').hide('fast');this works as well so i have no idea wtf is wrong. END EDIT

所以这是隐藏机制的问题。但是在控制台中:$('.langselmain').hide('fast');这也有效,所以我不知道 wtf 是错误的。结束编辑

I'm trying to check for a language preference cookie, and if it exists to hide the language selection dialogue. this is what I'm using to check the cookie:

我正在尝试检查语言首选项 cookie,以及它是否存在以隐藏语言选择对话框。这是我用来检查 cookie 的内容:

if (document.cookie.indexOf("TR_LNG") >= 0) { $('.langselmain').hide('fast'); } 

and the content to hide:

以及要隐藏的内容:

<div id="langselectsplash" class="langselectsplash langselmain"><div id="select"><img src="http://sarvatma.org/wp-content/uploads/2012/08/sarvalogo2.png" /><p> 
        <style>#tr_setdeflang{display:none;}</style><div class="no_translate transposh_flags" ><a id="en" href="/about/" class="tr_active">English</a><a id="fr" href="/fr/about/">Fran?ais</a><a id="de" href="/de/about/">Deutsch</a><a id="ja" href="/ja/about/">日本語</a><a id="pl" href="/pl/about/">Polski</a><a id="es" href="/es/about/">Espa?ol</a></div>        </p></div></div><div id="langsplashfade" class="langselmain"></div> 

and its just not working

它只是不工作

回答by Adam Merrifield

You can use: https://github.com/carhartl/jquery-cookie

您可以使用:https: //github.com/carhartl/jquery-cookie

js to set the cookie:

js设置cookie:

$.cookie('TR_LNG', 'set the value');

js to hide:

js隐藏:

if ($.cookie('TR_LNG')) {
    $('.langselmain').hide();
}

Working jsfiddle:

工作jsfiddle:

http://jsfiddle.net/3ypED/1/

http://jsfiddle.net/3ypED/1/

Edit: no plugin http://jsfiddle.net/3ypED/4/

编辑:没有插件 http://jsfiddle.net/3ypED/4/

回答by shibin

You can check cookie exist or not by

您可以通过以下方式检查cookie是否存在

if (document.cookie.indexOf("Login_ID") < 0) {
}

document.cookie.indexOf("Login_ID") = -1means cookie expired document.cookie.indexOf("Login_ID") = 0means cookie found

document.cookie.indexOf("Login_ID") = -1表示 cookie 已过期 document.cookie.indexOf("Login_ID") = 0表示找到 cookie

回答by Hymanson5

ugh it was a stupid mistake. I didnt bind my function.

呃,这是一个愚蠢的错误。我没有绑定我的函数。

$(document).ready(function(){
//that code
});

fixed it.

修复。