gpt4 book ai didi

Javascript 不更新样式

转载 作者:行者123 更新时间:2023-11-28 12:51:36 25 4
gpt4 key购买 nike

为什么这行不通? 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 片段。

最佳答案

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

参见 Why does jQuery or a DOM method such as getElementById not find the element? , 基本相同。

关于Javascript 不更新样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16906939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com