【问题标题】:Jquery - save class state for multiple div #'s to a cookie?Jquery - 将多个 div # 的类状态保存到 cookie 中?
【发布时间】:2011-11-17 05:12:26
【问题描述】:

我正在尝试复制 http://mcfc.co.uk 上的 UI 效果撤销。我是 jQuery 新手,如何将这些“点击”的 div 的状态保存到 cookie 以显示隐藏的等等??

感谢您的帮助。

<script type="text/javascript">

$(document).ready(function(){
    $('.portlet').click( function(){ 
        var idtext = this.id;
        $(this).hide();
        $("[id*=" + idtext + "]").not(this).addClass('add'); 
    });

    $("#content-footer div").click( function(){
        var idtext = this.id;   
        $(this).removeClass('add');                 
        $("[id*=" + idtext + "]").not(this).show();
    }); 
})

</script>

HTML:

点击时隐藏/显示的DIV.....

<div class="column" id="col0">

<div class="portlet" id="p_0">
  <div class="portlet-header">Feeds</div>
  <div class="portlet-content">Lorem ipsum dolor sit amet, consectetuer adipiscing elit
  </div>
</div>
<div class="portlet" id="p_1">
  <div class="portlet-header">News</div>
  <div class="portlet-content">Lorem ipsum dolor sit amet, consectetuer adipiscing elit
  </div>
</div>

</div>

etc....

那个类被应用到的DIV......

<div id="content-footer">
  <div id="p_0"></div>
  <div id="p_1"></div>
  <div id="p_2"></div>
  <div id="p_3"></div>
  <div id="p_4"></div>
</div>

【问题讨论】:

  • 请编辑您的格式,先选择您问题中的代码,然后单击代码按钮..
  • 您的 HTML 中没有唯一 ID,使用 $("[id*="+ idtext +"]") 授予该 ID 可以,但我认为这不是一个好习惯。

标签: jquery click cookies save


【解决方案1】:

我发布了demo of what I think you want here。请务必包含jQuery cookie plugin

这是我使用的 HTML:

<div class="column" id="col0">

<div class="portlet" id="p_0">
  <div class="portlet-header">Feeds</div>
  <div class="portlet-content">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div>
</div>
<div class="portlet" id="p_1">
  <div class="portlet-header">News</div>
  <div class="portlet-content">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div>
</div>
<div class="portlet" id="p_2">
  <div class="portlet-header">Extra</div>
  <div class="portlet-content">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div>
</div>
<div class="portlet" id="p_3">
  <div class="portlet-header">Other</div>
  <div class="portlet-content">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div>
</div>
<div class="portlet" id="p_4">
  <div class="portlet-header">Last</div>
  <div class="portlet-content">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div>
</div>

</div>

<div id="content-footer">
  <div name="p_0">p0 - Feeds</div>
  <div name="p_1">p1 - News</div>
  <div name="p_2">p2 - Extra</div>
  <div name="p_3">p3 - Other</div>
  <div name="p_4">p4 - Last</div>
</div>

还有脚本:

$(document).ready(function(){
 var cookie = $.cookie("hidden");
 var hidden = cookie ? cookie.split("|").getUnique() : [];
 var cookieExpires = 7; // cookie expires in 7 days, or set this as a date object to specify a date

 // Remember content that was hidden
 $.each( hidden, function(){
  var pid = this; //parseInt(this,10);
  $('#' + pid).hide();
  $("#content-footer div[name='" + pid + "']").addClass('add');
 })

 // Add Click functionality
 $('.portlet').click(function(){
  $(this).hide();
  $("#content-footer div[name=" + this.id + "]").addClass('add');
  updateCookie( $(this) );
 });
 $("#content-footer div").click(function(){
  $(this).toggleClass('add');
  var el = $("div#" + $(this).attr('name'));
  el.toggle();
  updateCookie( el );
 });

 // Update the Cookie
 function updateCookie(el){
  var indx = el.attr('id');
  var tmp = hidden.getUnique();
  if (el.is(':hidden')) {
   // add index of widget to hidden list
   tmp.push(indx);
  } else {
   // remove element id from the list
   tmp.splice( tmp.indexOf(indx) , 1);
  }
  hidden = tmp.getUnique();
  $.cookie("hidden", hidden.join('|'), { expires: cookieExpires } );
 }
}) 

// Return a unique array.
Array.prototype.getUnique = function() {
 var o = new Object();
 var i, e;
 for (i = 0; e = this[i]; i++) {o[e] = 1};
 var a = new Array();
 for (e in o) {a.push (e)};
 return a;
}

// Fix indexOf in IE
if (!Array.prototype.indexOf) {
 Array.prototype.indexOf = function(obj, start) {
  for (var i = (start || 0), j = this.length; i < j; i++) {
   if (this[i] == obj) { return i; }
  }
  return -1;
 }
}

更新:在上述脚本末尾添加“indexOf”原型以修复 IE 错误

更新 #2:添加 cookieExpires 变量,使用数字设置天数,将其设置为 date() 以设置结束日期或“null”以将其设置为会话 cookie。

【讨论】:

  • Fudgey - 这个脚本完全实现了我想要做的事情。非常感谢 - 独特的 # 选择器采用了板载。谢谢。 stackoverflow = 一个好地方
  • 我在 IE 中遇到了这个问题,它不会在页面加载时显示 portlet,Mozilla 没问题??
  • 呃,#&$^% IE 不合作。我想我需要一些帮助来弄清楚为什么它适用于除 IE 之外的所有浏览器。
  • 好的,我明白了...显然 IE 的 indexOf javascript 函数是 foobared。感谢这篇文章 (stackoverflow.com/questions/1744310/…),我在代码末尾添加了一个修复程序。代码和演示已经更新!
  • 再次感谢 Fudgey,我不知道这一点,完美!
【解决方案2】:

由于您已经在使用 jQuery,您可以利用它并使用非常简单易用的插件 Cookie:

你可以在这里看到一些demos

或者这里:

$.cookie("myCookie", true);

alert($.cookie("myCookie")); // alerts true. but remember, it's always returned as a string.

更新使用示例:

$(document).ready(function(){
    $('.portlet').click(function(){ 
        var idtext = this.id;
        $(this).hide();
        $("[id*="+ idtext +"]").not(this).addClass('add'); 
        $.cookie(idtext, false);
    });

    $("#content-footer div").click(function(){
        var idtext = this.id;   
        $(this).removeClass('add');                 
        $("[id*="+ idtext +"]").not(this).show();
        $.cookie(idtext, true);
    }); 
})

如您所见,我们在 cookie 中设置了当前 id (idtext) 的可见状态,其值为 true 或 false。 加载这些 portlet 时,您可以检查 cookie(服务器端或客户端,您的选择)并根据需要为前端提供服务。如果您需要更多帮助,请告诉我 :-)

【讨论】:

  • 感谢您的建议 kordonme。查看这个插件,我不确定如何实现它以保存每个 div # 的状态 - 以及“每次”点击时等效的 div # 类?
  • 感谢您提供的示例 - 我将如何处理存储在“idtext”中的值并更新“portlet”和相关的 div # 客户端,没有后端?
  • 其实 fudgey 发布了一个完美的演示。这看起来正是您想要的 :-)
  • 感谢您为我指明正确的方向 - 帮助很大
猜你喜欢
  • 2011-12-30
  • 2013-08-16
  • 2014-08-16
  • 2011-10-26
  • 1970-01-01
  • 2014-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多