當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


HTML Style fontStyle用法及代碼示例


HTML DOM樣式fontStyle屬性用於動態設置或獲取元素的字體。

用法:

  • 設置fontStyle屬性:
    object.style.fontStyle = normal|italic|oblique|initial|inherit;
    
  • 要獲取fontStyle屬性值:
    object.style.height
    

屬性值:


描述
normal
italic
oblique
intial
inherit

返回值:給出元素的fontStyle的字符串。

例:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML | DOM Style fontStyle Property 
    </title> 
    <style> 
        #gfg { 
            color:green; 
            font-size:100px; 
            font-style:normal; 
        } 
    </style> 
</head> 
  
<body> 
    <center> 
  
        <button onclick="changeStyle()"> 
          Click to change style 
        </button> 
        <!-- Script to change the fontStyle -->
        <script> 
            function changeStyle() { 
                document.getElementById("gfg").style.fontStyle  
                                    = "italic"; 
            } 
        </script> 
  
        <p id="gfg"> 
            Geeks For Geeks 
        </p> 
    </center> 
</body> 
  
</html>

輸出
在單擊按鈕之前:
HTML DOM fontstyle before gfg

單擊按鈕後:
HTML DOM fontstyle after gfg
例:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML | DOM Style fontStyle Property 
    </title> 
    <style> 
        #gfg { 
            color:green; 
            font-size:50px; 
            font-style:normal; 
        } 
    </style> 
</head> 
  
<body> 
    <center> 
        <br> 
        <button onclick="changeStyle()"> 
          Click to change style 
        </button> 
        <br /> 
        <br /> 
        <!-- Script to change the fontStyle -->
        <script> 
            function changeStyle() { 
                document.getElementById("gfg").style.fontStyle  
                                    = "oblique"; 
            } 
        </script> 
  
        <div id="gfg"> 
            HTML DOM fontStyle Property 
        </div> 
    </center> 
</body> 
  
</html>

輸出:
在單擊按鈕之前:
DOM fontstyle before gfg

單擊按鈕後:DOM fontstyle after gfg

支持的瀏覽器:HTML | DOM樣式fontStyle屬性在下麵列出:

  • 穀歌瀏覽器1.0
  • 互聯網瀏覽器/E​​dge 4.0
  • Firefox 1.0
  • Opera 7.0
  • Safari 1.0


相關用法


注:本文由純淨天空篩選整理自roshalmoraes大神的英文原創作品 HTML | DOM Style fontStyle Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。