HTML DOM 样式 direction 属性

htmlweb developmentfront end technology更新于 2024/3/14 5:00:17

HTML DOM 样式 direction 属性用于指定或返回文本方向。其默认值为 ltr。

以下是语法 −

设置 direction 属性 −

object.style.direction = "ltr|rtl|initial|inherit"

以上属性值的解释如下 −

值
描述
ltr
这是默认值,文本从左向右流动。
rtl
文本从右向左流动。
initial
将此属性设置为初始值。
inherit
继承父级属性值

让我们来看一个 direction 属性的示例 −

示例

<!DOCTYPE html>
<html>
<head>
<style>
   div,span {
      margin: 3px;
      padding: 4px;
   }
   div{
      height: 50px;
      width: 540px;
      border: 2px solid black;
   }
   span {
      float: left;
      width: 500px;
      height:40px;
      background-color: lightblue;
   }
</style>
<script>
   function changeDir(){
      document.getElementById("DIV1").style.direction="rtl";
      document.getElementById("Sample").innerHTML="The above text direction is now right to left";
   }
</script>
</head>
<body>
   <div id="DIV1">
      <span>THIS IS SOME TEXT INSIDE A SPAN ELEMENT.</span>
   </div>
   <p>Change the above text direction present inside the span element by clicking the below button</p>
   <button onclick="changeDir()">Change Text Direction</button>
   <p id="Sample"></p>
</body>
</html>

输出

点击"更改文本方向"按钮 −


相关文章


有用资源