如何使用 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>
广告