如何在 jquery/css 中设置会话变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22607630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 02:00:22  来源:igfitidea点击:

How to set session variable in jquery/css?

jquerycsssession

提问by Dinesh Patil

jQuery(document).ready(function($){
    $("#a2").click(function(){
        $('body').css("cssText","font-size: 107% !important");
    });
});

jQuery(document).ready(function($){
    $("#a3").click(function(){
        $('body').css("cssText","font-size: 114% !important");
    });
});

jQuery(document).ready(function($){
    $("#a4").click(function(){
        $('body').css("cssText","font-size: 121% !important");
    });
});

I have given a2, a3, a4 ids to A, A+, A++ buttons. Whenever visitor of my site will click on any one of these buttons, font-size of whole site will change accordingly. Right now font-size is changing but when I refresh the page, again it is showing the default font-size. How can I do that? do I need to use session? and how I can use it in css or jquery ? I am new in website development. So please help me.

我已经给 A、A+、A++ 按钮提供了 a2、a3、a4 id。每当我网站的访问者点击这些按钮中的任何一个时,整个网站的字体大小都会相应地改变。现在字体大小正在改变,但是当我刷新页面时,它再次显示默认字体大小。我怎样才能做到这一点?我需要使用会话吗?以及如何在 css 或 jquery 中使用它?我是网站开发的新手。所以请帮助我。

采纳答案by Dinesh Patil

following code i have written and it is working perfectly. I am sharing it so that other can refer it.

以下代码是我编写的,并且运行良好。我正在分享它,以便其他人可以参考。

jQuery(document).ready(function($){
    var fontCookie = get_cookie('fontclass');
    if(fontCookie) {
        jQuery('body').addClass(fontCookie);
    }
    $("#a2").click(function(){
        jQuery('body').removeClass('body-18');
        jQuery('body').addClass('body-16');
        var now = new Date();
            var time = now.getTime();
            time += 3600 * 1000 * 700;
            now.setTime(time);
            document.cookie = 
            'fontclass=body-16' + 
            '; expires=' + now.toGMTString() + 
            '; path=/';
        event.preventDefault();
    });
});

like this I have added function for a1 and a3 also in which I am adding and removing css classes as per my requirement.

像这样,我为 a1 和 a3 添加了函数,我也在其中根据我的要求添加和删除了 css 类。

回答by Ashwin Balani

Whenever you would be setting up a session variable, you must use AJAX to send a server side request, it is very light weight and does not require to load any external libraries

每当您设置会话变量时,您都必须使用 AJAX 发送服务器端请求,它的重量非常轻,不需要加载任何外部库

回答by Fabien

Session are stored on server. So, if you want to use session, you must use ajax to get and set information.

会话存储在服务器上。所以,如果要使用session,就必须使用ajax来获取和设置信息。

Easier solution is to use cookies that you can manage in javascript. see http://www.w3schools.com/js/js_cookies.asp

更简单的解决方案是使用您可以在 javascript 中管理的 cookie。见http://www.w3schools.com/js/js_cookies.asp

回答by Waqas Memon

To be specific to the question: jquery.session.js Plugin serves the purpose it requires JSON Plugin too http://code.google.com/p/jquery-json/

具体到这个问题:jquery.session.js Plugin 的目的是它也需要 JSON Plugin http://code.google.com/p/jquery-json/

Store:

店铺:

$(function() {
     $.session(“myVar, “value”);
});

Read:

读:

$.session(“myVar)

If i didnt get you right, and if my above answer doesnt help your situation, your description says you want to store personalization information of the user, which is normally and usually either stored in cookies that expire after very long times or they are stored in databases at the back-end.

如果我没有说对你,并且如果我上面的回答对你的情况没有帮助,你的描述说你想存储用户的个性化信息,这通常通常要么存储在很长时间后过期的 cookie 中,要么存储在后端的数据库。