如何通过 JavaScript 更改字体大小?
该代码不起作用
var span = document.getElementById("span");
span.style.fontsize = "25px";
span.innerHTML = "String";
This code is not working
var span = document.getElementById("span");
span.style.fontsize = "25px";
span.innerHTML = "String";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
JavaScript 区分大小写。
所以,如果你想改变字体大小,你必须这样做:
JavaScript is case sensitive.
So, if you want to change the font size, you have to go:
您的代码中有两个错误:
document.getElementById
-这将检索 ID 为“span”的元素,您没有在 span 元素上指定 id。
Javascript 中的大写字母 -
你还忘记了 Size 的大写。
You have two errors in your code:
document.getElementById
-This retrieves the element with an Id that is "span", you did not specify an id on the span-element.
Capitals in Javascript -
Also you forgot the capital of Size.
试试这个:
try this:
请永远不要在实际项目中这样做
Please never do this in real projects????:
用这个
use this
我遇到了同样的问题,这是我原来的 HTML:
这是我原来的 JS:
这是我的新 JS,它似乎工作得很好:
所以出于某种原因,我不得不在最后作为一根绳子,不知何故它起作用了。我希望这有帮助:)
I had the same problem, this was my original HTML:
And this was my original JS:
This is my new JS, and it seems to be working just fine:
So for some reason, I had to add the little "px" at the very last as a string, and somehow it worked. I hope this helps :)