简体   繁体   English

如何使用jquery cookie插件确定cookie是否存在?

[英]How to determine if a cookie exists with the jquery cookie plugin?

I can save a cookie like this $.cookie('position' + $(this).index('li').toString(), currentTop.toString());我可以保存这样的 cookie $.cookie('position' + $(this).index('li').toString(), currentTop.toString());

And I'm able to delete a cookie with this $.cookie('position' + $(this).index('li').toString(), null);我可以用这个$.cookie('position' + $(this).index('li').toString(), null);删除一个 cookie $.cookie('position' + $(this).index('li').toString(), null);

However how can I query if one of those cookies exists?但是,如何查询这些 cookie 之一是否存在?

if ( $.cookie('position'+ $(this).index('li').toString()) == null ) {

thank you谢谢你

update/edit:更新/编辑:

I have horizontal list-elements (absolute position and 100% width) that are draggable.我有可拖动的水平列表元素(绝对位置和 100% 宽度)。 When dragged (and dropped) I want to save the position of each list-element with a cookie so the site remembers the position of each element.当拖动(和放下)时,我想用 cookie 保存每个列表元素的位置,以便网站记住每个元素的位置。

<ul class="bars">  
    <li><a href="home">Some Name</a></li>  
    <li class="page_item"><a href="#" title="Downloads">Downloads</a></li>  
    <li class="page_item"><a href="#" title="Contact">Contact</a></li>  
    <li class="page_item"><a href="#" title="Work">Work</a></li> 
</ul>  

Example: http://jsfiddle.net/wa9Ga/ As explained above I want to store each item's position with a cookie.示例: http : //jsfiddle.net/wa9Ga/如上所述,我想用 cookie 存储每个项目的位置。 And now to my question above - I want to distribute each list-item randomly if the visitor is visiting the website for the first time (so no cookie has been set).现在回答我上面的问题 - 如果访问者是第一次访问网站,我想随机分发每个列表项(因此没有设置 cookie)。

How can I solve this.我该如何解决这个问题。

if you want to test if a cookie exists you can simply do this:如果您想测试 cookie 是否存在,您可以简单地执行以下操作:

var cookie_name = 'position'+ $(this).index('li').toString();

if ($.cookie(cookie_name)) {
    do_something();
}
else {
    do_something_else();
}

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

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