How to change FontSize By JavaScript?

后端 未结 5 1712
独厮守ぢ
独厮守ぢ 2020-11-27 21:45

This code is not working

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

<

相关标签:
5条回答
  • 2020-11-27 21:46
    <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.

    0 讨论(0)
  • 2020-11-27 21:55

    JavaScript is case sensitive.

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

    span.style.fontSize = "25px";
    
    0 讨论(0)
  • 2020-11-27 21:57

    Please never do this in real projects

    0 讨论(0)
  • 2020-11-27 22:05
    span.style.fontSize = "25px";
    

    use this

    0 讨论(0)
  • 2020-11-27 22:08

    try this:

    var span = document.getElementById("span");
    span.style.fontSize = "25px";
    span.innerHTML = "String";
    
    0 讨论(0)
提交回复
热议问题