[英]JavaScript font-size cookie
I'm kind of new to JavaScript. 我对JavaScript有点陌生。 I've created a function that changes the font size and I would like to save the size in a cookie, so that the user won't have to change it every time. 我创建了一个可以更改字体大小的函数,并且我希望将字体大小保存在cookie中,以便用户不必每次都更改它。 But I haven't got the cookie part to work. 但是我还没有cookie的一部分。
I'm using the following libraries: jQuery JavaScript Library v1.11.1 and https://developer.mozilla.org/en-US/docs/Web/API/document.cookie 我正在使用以下库:jQuery JavaScript Library v1.11.1和https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
function change_size(element, size) {
var new_size;
var current = parseInt(docCookies.getItem("FontSize"));
if (current !== "") {
current = parseInt(element.css('font-size'));
}
if (size === 'smaller') {
if (current > 12) {
new_size = current - 2;
}
}
else if (size === 'bigger') {
if (current < 22) {
new_size = current + 2;
}
}
element.css('font-size', new_size + 'px');
docCookies.setItem("FontSize", num.toString(new_size), Infinity);
}
$('#smaller').click(function() {
change_size($('p'), 'smaller');
});
$('#bigger').click(function() {
change_size($('p'), 'bigger');
});
Change this line 更改此行
docCookies.setItem("FontSize", num.toString(new_size), Infinity);
to 至
docCookies.setItem("FontSize", new_size, Infinity);
You don't have any num
variable. 您没有任何num
变量。
Also don't forget to set new font size on page load: 同样不要忘记在页面加载时设置新的字体大小:
var fontSize = docCookies.getItem("FontSize");
if (fontSize) {
$('p').css('font-size', fontSize + 'px');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.