HTML DOM Bdo dir 屬性


HTML DOM Bdo dir 屬性與 HTML <bdo> 元素相關。其中,bdo 代表雙向覆蓋。<bdo> 標籤用於覆蓋當前文字方向,預設情況下為從左到右。bdo dir 屬性設定或返回 <bdo> 元素的 dir 屬性值。dir 屬性對於 <bdo> 元素是必須的。它指定文字流的方向。

語法

以下是語法:

設定 dir 屬性:

bdoObject.dir = "ltr|rtl"

這裡,ltr 是從左到右的文字方向,而 rtl 是從右到左的文字方向。

返回 dir 屬性:

bdoObject.dir

示例

讓我們來看一個 HTML DOM bdo dir 屬性的示例:

<!DOCTYPE html>
<html>
<body>
<h3><bdo id="myBdo" dir="rtl">RIGHT-TO-LEFT</bdo></h3>
<p>Click the below button to get text direction of the above text</p>
<button onclick="getDirection()">GET DIRECTION</button>
<button onclick="setDirection()">SET DIRECTION</button>
<p id="Sample"></p>
<script>
   function getDirection() {
      var x = document.getElementById("myBdo").dir;
      document.getElementById("Sample").innerHTML ="The text direction is from " + x;
   }
   function setDirection(){
      document.getElementById("myBdo").dir="ltr";
   }
</script>
</body>
</html>

輸出

這將產生以下輸出:

點選“獲取方向”後:

點選“設定方向”後:

在以上示例中:

我們首先在 <h3> 元素內部建立了一個  元素,並將 dir 屬性值設定為“rtl”:

<h3><bdo id="myBdo" dir="rtl">RIGHT-TO-LEFT</bdo></h3>

然後,我們建立了兩個按鈕“獲取方向”和“設定方向”,分別執行 getDirection() 和 setDirection() 函式:

<button onclick="getDirection()">GET DIRECTION</button>
<button onclick="setDirection()">SET DIRECTION</button>

getDirection() 函式獲取與其關聯的 id 為“myBdo”的元素,在本例中為 <bdo> 元素。然後,從  元素獲取的 dir 屬性值被賦值給變數 x。然後,該值顯示在 id 為“Sample”的段落中:

function getDirection() {
   var x = document.getElementById("myBdo").dir;
   document.getElementById("Sample").innerHTML ="The text direction is from " + x;
}

setDirection() 函式透過 id 獲取元素“mybdo”,並將它的 dir 屬性值設定為“ltr”,這意味著從左到右。這也是預設的文字方向:

function setDirection(){
   document.getElementById("myBdo").dir="ltr";
}

更新於: 2019年8月6日

104 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.