当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。