如何用 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>

更新于: 2020 年 6 月 23 日

110 次浏览

开启您的 职业生涯

完成课程即可获得认证

开始学习
广告