我需要一些有关本地存储的帮助。到目前为止,我有一些 js 允许用户更改 div 的字体系列,并且它设法在本地保存用户的选择。但是,我不知道如何在重新加载时将字体系列值(即 helvetica)应用于 div。
function updateh1family() {
var selector = document.getElementById('selecth1FontFamily');
var family = selector.options[selector.selectedIndex].value;
var h1 = document.getElementById('edit')
h1.style.fontFamily = family;
var font = document.getElementById('edit');
font.onchange = function() {
document.getElementById('edit').style.fontFamily = family;
localStorage.setItem('font', family);
}
font.value = localStorage.getItem('font');
font.onchange();
if (localStorage.length != 0) {
document.getElementById('edit').style.fontFamily = localStorage.font;
document.getElementById('edit').value = localStorage.font;
}}
最佳答案
检查此解决方案:http://jsfiddle.net/jy424/2/
var selector = document.getElementById('selecth1FontFamily'),
text= document.getElementById('edit');
if (localStorage.length != 0) {
text.style.fontFamily = localStorage.font;
text.value = localStorage.font;
}else{
localStorage.setItem('font', '');
}
selector.onchange = function(){
var family = this.value;
text.style.fontFamily = family;
localStorage.font = family;
}
关于javascript - 本地存储字体系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20598036/