如何使用 JavaScript 设置或返回应该将元素分成多少列?


要将 div 分成三列,请使用 columnCount 属性。设置列计数并划分 div。

示例

你可以尝试运行以下代码,以 JavaScript 返回应该将元素分成多少列 −

实时演示

<!DOCTYPE html>
<html>
   <body>
      <p>Click below to create 4 columns</p>
      <button onclick="display()">Columns</button>
      <div id="myID">
         This is demo text. This is demo text. This is demo text. This is demo text.
         This is demo text. This is demo text. This is demo text. This is demo text.
         This is demo text. This is demo text. This is demo text. This is demo text.
         This is demo text. This is demo text. This is demo text. This is demo text.
         This is demo text. This is demo text. This is demo text. This is demo text.
         This is demo text. This is demo text. This is demo text. This is demo text.
         This is demo text. This is demo text. This is demo text. This is demo text.
      </div>
      <script>
         function display() {
            document.getElementById("myID").style.columnCount = "4";
         }
      </script>
   </body>
</html>

更新于:23-06-2020

110 个浏览量

开启您的 职业生涯

完成课程以获得认证

入门
广告