HTML - DOM樣式物件direction屬性



HTML DOM樣式物件direction屬性用於設定或獲取元素的文字方向。

語法

設定direction屬性
object.style.direction= "ltr | rtl | initial | inherit";
獲取direction屬性
object.style.direction;

屬性值

描述
ltr 這是預設值,指定從左到右的文字方向。
rtl 指定從右到左的文字方向。
initial 用於將此屬性設定為其預設值。
inherit 用於繼承其父元素的屬性。

返回值

它返回一個字串值,表示元素的文字方向。

HTML DOM樣式物件“direction”屬性示例

以下示例將方向從從左到右更改為從右到左。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object direction Property
    </title>
</head>
<body>
    <button onclick="fun()">RTL</button>
    <button onclick="funTwo()">LTR</button>
    <p id="direction">Welcome to Tutorials Point</p>
    <script>
        function fun() {
            document.getElementById("direction")
                .style.direction = "rtl";
        }
        function funTwo() {
            document.getElementById("direction")
                .style.direction = "ltr";
        }
    </script>
</body>
</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
direction 是 2 是 12 是 1 是 1 是 9.2
html_dom_style_object_reference.htm
廣告