使用cookie来切换身体类

时间:2017-06-12 10:42:56

标签: javascript jquery cookies

我正在为寻找一个有效的解决方案而抨击我。 目标是为视力不佳的人创建一个文本大小的切换器。 我创建了三个span元素,其中小,中,大类。

我有一段代码几乎可以完成工作,但它需要cookie部分。

$(function() {
    $(".font-toggle span").click(function() {
        var size = $(this).attr('class');
        $("body").attr("id", size);
        return false;
    });    
});

如何使用Cookie保存我的选择并在页面刷新后添加? 一直在阅读许多关于设置cookie的stackoverflow帖子,但所有这些帖子只有一个切换。我有三个。

JSFIDDLE HERE

非常感谢!

3 个答案:

答案 0 :(得分:0)

$(document).ready(function(){
    $(".font-toggle span").click(function() {
        var size = $(this).attr('class');
        $("body").attr("id", size);
        if($.cookie('mycookie') == 'valueOfCookie'){
           $.cookie('mycookie', '');
        }
        else{
          $.cookie('mycookie', 'valueOfCookie');
        }
        
        return false;
    }); 
    if($.cookie('mycookie') == 'valueOfCookie'){
       $("body").attr("id", 'big-font');
    }
    else{
      $("body").removeAttr("id");
    }
});
#big-font{
  font-size: 40px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="https://github.com/carhartl/jquery-cookie/releases/download/v1.4.1/jquery.cookie-1.4.1.min.js"></script>


<div class="font-toggle">
  <span class="big-font">
   Button
  </span>
</div>
<div>
  Text To Show
</div>

答案 1 :(得分:0)

也许这对你有用。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="https://github.com/carhartl/jquery-cookie/releases/download/v1.4.1/jquery.cookie-1.4.1.min.js"></script>
<script>
  "use strict";


  $(document).ready(function() {
    let eyesight = $.cookie('eyesight');

    if (eyesight != undefined) {
      $("body").addClass(eyesight);
    } else 
      setEyesight('medium'); // Default size

    $(".font-toggle span").click(function() {
      setEyesight($(this).attr('class'));
    });
  });


  function setEyesight(size) {
    let eyesight = $.cookie('eyesight');

    if (eyesight != undefined) {
      $("body").removeClass(eyesight);
      $.removeCookie('eyesight');
    }

    $("body").addClass(size);
    $.cookie('eyesight', size);
  }
</script>

答案 2 :(得分:0)

这个怎么样?

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="https://github.com/carhartl/jquery-cookie/releases/download/v1.4.1/jquery.cookie-1.4.1.min.js"></script>
<script>
  "use strict";

  $(document).ready(function() {
    let eyesight = new Eyesight();
    eyesight.initialize();

    $(".font-toggle span").click(function() {
      eyesight.set($(this).attr('class'));
    });
  });

  function Eyesight() {
    let self = this;
    let name = "eyesight";
    let current = null;
    let _default = "medium";
    let data = $.cookie(name);

    // initialzie
    this.initialize = function() {
      if (data!= undefined) {
        $("body").addClass(data);
        current = data;
      } else 
        self.set(_default); // Default size

      // init cookie listener
      self.listener();
    };

    // set eyesight function
    this.set = function(size) {
      if (data != undefined) {
        $("body").removeClass(data);
        $.removeCookie(name);
      }

      $("body").addClass(size);
      $.cookie(name, size);
      current = size;
    };

    // eyesight cookie listener function
    this.listener = function() {
      setTimeout(function() {
        let cookie = $.cookie(name);

        if (cookie != undefined && cookie != current)
          self.set(cookie);

        self.listener();
      }, 100);
    };
  }
</script>