添加Cookie以维护font-size跨jQuery移动页面 [英] Add Cookie to Maintain font-size Across jQuery Mobile Pages

查看:118
本文介绍了添加Cookie以维护font-size跨jQuery移动页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向此代码添加Cookie请求,以在jQM网页更改后保持用户选择的字体大小。从来没有这样做过。任何人都可以让我开始?

 < script& 
$(document).ready(function(){
$(。increaseFont,.decreaseFont)。click(function(){
var type = $(this).val );
var curFontSize = $('。gridContainer')。css('font-size');
if(type =='increase'){
$('gridContainer' )css('font-size',parseInt(curFontSize)+ 2);
} else {
$('gridContainer' 2);
}

});


});
< / script>感谢一如既往!

方案

为什么不使用localStorage而不是cookie?它永远不会过期,并且真的很容易在JavaScript中使用。使用setItem存储字体大小:

  localStorage.setItem('FontSize',curFontSize); 

然后在另一页上检索字体大小:

  var curFontSize = localStorage [FontSize]; 

如果以后要删除保存的字体大小:

  localStorage.removeItem(FontSize); 

这里有一些关于localStorage的文档: http://www.w3schools.com/html/html5_webstorage.asp


I'm trying to add a cookie request to this code to maintain the user's selected font-size across jQM pages once they change it. Never done this before. Can anybody get me started?

<script>
$(document).ready(function () {
    $(".increaseFont,.decreaseFont").click(function () {
        var type = $(this).val();
        var curFontSize = $('.gridContainer').css('font-size');
        if (type == 'increase') {
            $('.gridContainer').css('font-size', parseInt(curFontSize) + 2);
        } else {
            $('.gridContainer').css('font-size', parseInt(curFontSize) - 2);
        }

    });


});
</script>

Thanks as always!

解决方案

Instead of a cookie, why not use localStorage? It never expires and is really easy to use within JavaScript. Use setItem to store the font size:

localStorage.setItem('FontSize', curFontSize);

Then to retrieve the font size on another page:

var curFontSize= localStorage["FontSize"];

If later you want to remove the saved font size:

localStorage.removeItem("FontSize");

Here is some documentation on localStorage: http://www.w3schools.com/html/html5_webstorage.asp

这篇关于添加Cookie以维护font-size跨jQuery移动页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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