如何使用 JavaScript 設定字型的樣式為正常、斜體或傾斜?


要設定字型的樣式,請使用 fontStyle 屬性。你可以嘗試執行以下程式碼以使用 JavaScript 設定字型的樣式為 normal、italic 或 oblique −

示例

線上演示

<!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 Style</button>
      <script>
         function display() {
            document.getElementById("myID").style.fontFamily = "verdana,sans-serif";
            document.getElementById("myID").style.fontStyle = "italic";
         }
      </script>
   </body>
</html>

更新於: 2020 年 6 月 23 日

92 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.