如何通过 JavaScript 更改字体大小?

发布于 2024-10-31 05:21:34 字数 160 浏览 3 评论 0原文

该代码不起作用

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浅唱々樱花落 2024-11-07 05:21:34

JavaScript 区分大小写。

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

span.style.fontSize = "25px";

JavaScript is case sensitive.

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

span.style.fontSize = "25px";
眼泪都笑了 2024-11-07 05:21:34
<span id="span">HOI</span>
<script>
   var span = document.getElementById("span");
   console.log(span);

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

您的代码中有两个错误:

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

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

<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.

荒路情人 2024-11-07 05:21:34

试试这个:

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

try this:

var span = document.getElementById("span");
span.style.fontSize = "25px";
span.innerHTML = "String";
夜未央樱花落 2024-11-07 05:21:34

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

Please never do this in real projects????:

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

别挽留 2024-11-07 05:21:34
span.style.fontSize = "25px";

用这个

span.style.fontSize = "25px";

use this

梦里°也失望 2024-11-07 05:21:34

我遇到了同样的问题,这是我原来的 HTML:

<input id = "fsize" placeholder = "Font Size" type = "number">
</input>

<button id = "apfsizeid" onclick = "apfsize()"> Apply Font Size 
</button>

<textarea id = "tarea"> Your Text 
</textarea>

这是我原来的 JS:

function(apfsize) {
var fsize = document.getElementById("fsize").value;
var text = document.getElementById("tarea").innerHTML;
text.style.fontsize = fsize;
document.getElementById("tarea").innerHTML = text;
}

这是我的新 JS,它似乎工作得很好:

function apfsize() {
var fsize = document.getElementById("fsize").value;
fsize = Number(fsize);
document.getElementById("tarea").style.fontSize = fsize + "px";
}

所以出于某种原因,我不得不在最后作为一根绳子,不知何故它起作用了。我希望这有帮助:)

I had the same problem, this was my original HTML:

<input id = "fsize" placeholder = "Font Size" type = "number">
</input>

<button id = "apfsizeid" onclick = "apfsize()"> Apply Font Size 
</button>

<textarea id = "tarea"> Your Text 
</textarea>

And this was my original JS:

function(apfsize) {
var fsize = document.getElementById("fsize").value;
var text = document.getElementById("tarea").innerHTML;
text.style.fontsize = fsize;
document.getElementById("tarea").innerHTML = text;
}

This is my new JS, and it seems to be working just fine:

function apfsize() {
var fsize = document.getElementById("fsize").value;
fsize = Number(fsize);
document.getElementById("tarea").style.fontSize = fsize + "px";
}

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文