如何使用 JavaScript 設定文字的字型大小?


要設定字型大小,請在 JavaScript 中使用 fontSize 屬性。你可以嘗試執行以下程式碼來使用 JavaScript 設定文字的字型大小 −

示例

線上演示
<!DOCTYPE html>
<html>
   <body>
      <h1>Heading 1</h1>
      <p id = "myID">
         This is Demo Text. This is Demo Text. This is Demo Text. This is Demo Text.
         This is Demo Text. This is Demo Text. This is Demo Text. This is Demo Text.
      </p>
      <button type = "button" onclick = "display()">Set Font Family and Size</button>
      
      <script>
         function display() {
            document.getElementById("myID").style.fontFamily = "verdana,sans-serif";
            document.getElementById("myID").style.fontSize = "smaller";
         }
      </script>
      
   </body>
</html>

更新於: 2019 年 7 月 30 日

800 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.