如何使用toggle()在jquery中设置cookie [英] How to set a cookie in jquery using toggle()

查看:110
本文介绍了如何使用toggle()在jquery中设置cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户点击链接时查找要设置的cookie,它将打开div,然后用户可以刷新页面并看到div仍处于打开状态。

Looking for a cookie to be set when the user clicks on the link it'll open the div then the user can refresh the page and see the div is still open.

======= HTML =======

=======HTML=======

<a class="show-settings" href="#"></a>

======== jQuery =======

========jQuery=======

$(function () {
//Toggle Settings
var s = $("a.show-settings"); 

//On click to toggle settings
s.click(function () {
    $("#s4-ribbonrow, #s4-titlerow").toggle();
});
//Add/Remove text
s.toggle(function () {
    //$(this).text("Hide Settings");
}, function () {
    //$(this).text("Show Settings");
});


推荐答案

使用jquery-cookies和来自切换的回调功能

Something like this using jquery-cookies and the callback functionality from toggle

$(document).ready(function() {
 SetView();

 $('.show-settings').click(function() {
  $('#s4-ribbonrow, #s4-titlerow').toggle(0, function(){$.cookie('show-settings', $("#s4-ribbonrow:visible")});
 });

 function SetView() {
  if ($.cookie('loginScreen') == 'true')
   $('#s4-ribbonrow, #s4-titlerow').show();
 }
}

这篇关于如何使用toggle()在jquery中设置cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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