当链接点击使Div出现并保存隐藏/显示选项在$ .cookie [英] When link clicked make a Div appear and save the hide/show options in $.cookie

查看:266
本文介绍了当链接点击使Div出现并保存隐藏/显示选项在$ .cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试完成的是当点击链接使一个div可见,并使其保持在那里,直到它被点击,无论窗口刷新。还保存在cookie中的显示/隐藏选项

What i m trying to accomplish is when a link is clicked to make a div visible and make it remain there until its clicked no matter of window refreshes.Also save the show/hide options in a cookie

    < ahref="#">Home< / a>
    < div id="contentwrapper" style="display:hidden">
        holas
    < /div>

$(document).ready(function() {



 $('a').click(function(){

  $('#contentwrapper').fadeIn(300);        

  $.cookie('content','visible');
 var thecontent = $.cookie('content');

 });

 $('#contentwrapper').click(function(){
     $('#contentwrapper').fadeOut(100);
    $.cookie('content','hidden');

 });


 if ( thecontent == 'visible'){
   $('#contentwrapper').css("display","block");

  };
  if (content == 'hidden'){
   $('#contentwrapper').fadeOut(200);
  };

});


推荐答案

很少事情:


  • 没有显示类型隐藏 style =display:hidden,请改用 none

  • 当页面加载时,未在 if(thecontent =='visible'){中定义变量到 if($ .cookie('content')=='visible'){

  • c> hidden if($ .cookie('content')=='hidden'){

  • 如果你使用 fadeOut 等从jQuery,你应该使用 $('#contentwrapper')。show(); 而不是 $('#contentwrapper')。css(display,block);

  • There is no display type of hidden as you specify in style="display:hidden", use none instead.
  • When the page loads, thecontent variable is not defined in if ( thecontent == 'visible'){ so change it to if ( $.cookie('content') == 'visible'){
  • Same thing with the hidden: if ($.cookie('content') == 'hidden'){
  • If you are using fadeOut etc. from jQuery, you should use $('#contentwrapper').show(); instead of $('#contentwrapper').css("display","block");

所有:

$('a').click(function(){   
    $('#contentwrapper').fadeIn(300);        
    $.cookie('content','visible');
 });

 $('#contentwrapper').click(function(){
    $('#contentwrapper').fadeOut(100);
    $.cookie('content','hidden');
 });


 if ( $.cookie('content') == 'visible'){
   $('#contentwrapper').show();    
  };

 if ($.cookie('content') == 'hidden'){
   $('#contentwrapper').fadeOut(200);
 };

这篇关于当链接点击使Div出现并保存隐藏/显示选项在$ .cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆