如何使用 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 個瀏覽量

開啟您的 職業生涯

完成課程以獲得認證

入門
廣告
© . All rights reserved.