HTML DOM Style order 属性

htmldomweb developmentfront end technology更新于 2024/5/23 17:48:17

HTML DOM Style order 属性返回并修改 HTML 文档中同一容器内灵活项目相对于其余灵活项目的顺序。

语法

以下是语法 −

1. 返回 order

object.order

2. 修改 order

object.order = “value”

此处,值可以为 -

值
说明
initial
将此属性值设置为其默认值。
inherit
从其父元素继承此属性值。
number
它表示指定灵活项目顺序的数字。

让我们看一个 HTML DOM Style order 属性的示例 −

示例

<!DOCTYPE html>
<html>
<style>
   body {
      color: #000;
      height: 100vh;
      background-color: #8BC6EC;
      background-image: linear-gradient(135deg, #8BC6EC 0%, #9599E2 100%);
   }
   .btn {
      background: #db133a;
      border: none;
      height: 2rem;
      border-radius: 2px;
      width: 40%;
      display: block;
      color: #fff;
      outline: none;
      cursor: pointer;
      margin: 1rem auto;
   }
   .container {
      border: 3px solid #fff;
      margin: 1rem auto;
      display: flex;
      padding: 2rem;
   }
   .box {
      width: 100px;
      height: 100px;
   }
</style>
<body>
<h1 style="text-align:center">DOM Style order Property Demo</h1>
<div class="container">
<div class="box" id="b1" style="background-color:#7F055F;"></div>
<div class="box" id="b2" style="background-color:#3A015C;"></div>
<div class="box" id="b3" style="background-color:#177E89;"></div>
</div>
<button onclick="set()" class="btn">Change order</button>
<script>
   function set() {
      document.querySelector('#b1').style.order = "3";
      document.querySelector('#b2').style.order = "2";
      document.querySelector('#b3').style.order = "1";
   }
</script>
</body>
</html>

输出

点击"更改顺序"按钮可更改白色边框容器内的灵活项目的顺序。


相关文章


有用资源