如何使用 JavaScript 设置项目相对于其余部分的长度?

javascriptobject oriented programmingfront end technology更新于 2024/6/27 3:24:17

使用 JavaScript 中的 flex 属性设置项目相对于其余部分的长度。您可以尝试运行以下代码以使用 JavaScript 实现 flex 属性 −

示例

<!DOCTYPE html>
<html>
   <head>
      <style>
         #box {
            border: 1px solid #000000;
            width: 300px;
            height: 400px;
            display: flex;
         }
      </style>
   </head>
   <body>
      <div id = "box">
         <div style = "background-color:orange;">DIV1</div>
         <div style = "background-color:blue;">DIV2</div>
         <div style = "background-color:yellow;">DIV3</div>
      </div>

      <button onclick = "display()">Set</button>

      <script>
         function display() {

            var a = document.getElementById("box");
            var b = a.getElementsByTagName("DIV");

            var j ;
            for (j = 0; j < b.length; j++) {
               b[j].style.flex = "1";
            }
         }
      </script>
   </body>
</html>

相关文章


有用资源