问题
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!
回答1:
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
回答2:
I woudl recommend jQuery.Cookie to work with Javascript and Cookies, otherwise it can be a mess... https://github.com/carhartl/jquery-cookie
来源:https://stackoverflow.com/questions/20204206/add-cookie-to-maintain-font-size-across-jquery-mobile-pages