HTML - DOM Style 物件 fontStyle 屬性



HTML DOM Style 物件的 **fontStyle** 屬性設定或返回元素的字型樣式。

語法

設定 fontStyle 屬性
object.style.fontStyle= "normal | italic | oblique | initial | inherit";
獲取 fontStyle 屬性
object.style.fontStyle;

屬性值

描述
normal 這是預設值,表示元素的普通字型。
italic 表示斜體字型。
oblique 表示傾斜字型。
initial 用於將此屬性設定為其預設值。
inherit 用於繼承其父元素的屬性。

返回值

它返回一個字串值,表示元素的字型樣式屬性。

HTML DOM Style 物件“fontStyle”屬性示例

以下示例實現了字型樣式屬性的斜體、普通和傾斜值。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object fontStyle Property
    </title>
</head>
<body>
    <p>Click to change font style.</p>
    <button onclick="italic()">Italic</button>
    <button onclick="normal()">Normal</button>
    <button onclick="oblique()">Oblique</button>
    <p id="font">Welcome to Tutorials Point.</p>    
    <script>
        function italic() {
            document.getElementById("font")
                .style.fontStyle= "italic";
        }
        function normal() {
            document.getElementById("font")
                .style.fontStyle= "normal";
        }
        function oblique() {
            document.getElementById("font")
                .style.fontStyle= "oblique";
        }
    </script>
</body>
</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
fontStyle 是 1 是 12 是 1 是 1 是 7
html_dom_style_object_reference.htm
廣告