如何用 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 次瀏覽

開啟您的 職業生涯

完成課程即可獲得認證

開始學習
廣告