0

为什么这不起作用? localStorage.fontSize包含正确的值,但第一个document.body.style.fontSize = localStorage.fontSize + "pt";不更新样式。

<script type="text/javascript">

if(!localStorage.fontSize){
    localStorage.fontSize = Number(11); 
}
else{
    document.body.style.fontSize = localStorage.fontSize + "pt"; /* This row doesnt run */
}

function resetFont(){
    localStorage.fontSize = Number(11);
    document.body.style.fontSize = "11pt";
}
function enlargeFont(){
    localStorage.fontSize++;
    document.body.style.fontSize = localStorage.fontSize + "pt"; /* But this does if called */
}

</script>

请不要使用 jQuery 片段。

4

1 回答 1

0

似乎document.body在代码运行时不存在。将代码移动到body标记的开头或结尾。

请参阅为什么 jQuery 或诸如 getElementById 之类的 DOM 方法找不到元素?,基本上是一样的。

于 2015-04-29T22:36:50.477 回答