如何通过 JavaScript 更改 FontSize?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5586703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:56:27  来源:igfitidea点击:

How to change FontSize By JavaScript?

javascriptcssfontsfont-size

提问by jams

This code is not working

此代码不起作用

var span = document.getElementById("span");
span.style.fontsize = "25px";
span.innerHTML = "String";



回答by ambiguousmouse

JavaScript is case sensitive.

JavaScript 区分大小写。

So, if you want to change the font size, you have to go:

所以,如果你想改变字体大小,你必须去:

span.style.fontSize = "25px";

回答by Mark

<span id="span">HOI</span>
<script>
   var span = document.getElementById("span");
   console.log(span);

   span.style.fontSize = "25px";
   span.innerHTML = "String";
</script>

You have two errors in your code:

您的代码中有两个错误:

  1. document.getElementById- This retrieves the element with an Id that is "span", you did not specify an id on the span-element.

  2. Capitals in Javascript - Also you forgot the capital of Size.

  1. document.getElementById- 这将检索 ID 为“span”的元素,您没有在 span 元素上指定 ID。

  2. Javascript 中的大写字母 - 你也忘记了 Size 的大写字母。

回答by Naftali aka Neal

try this:

尝试这个:

var span = document.getElementById("span");
span.style.fontSize = "25px";
span.innerHTML = "String";

回答by tibalt

Please never do this in real projects:

请不要在实际项目中这样做:

document.getElementById("span").innerHTML = "String".fontsize(25);
<span id="span"></span>