如何利用 JavaScript 設定元素的最小高度?


在 JavaScript 中使用<em>minHeight</em>屬性設定最大高度。你可以嘗試執行以下程式碼,利用 JavaScript 設定元素的最小高度 −

示例

線上演示

<!DOCTYPE html>
<html>
   <head>
      <style>
         #box {
            width: 300px;
            background-color: gray;
            overflow: auto;
         }
      </style>
   </head>
   <body>
      <p>Click below to set Minimum height.</p>
      <button type="button" onclick="display()">Min Height</button>
      <div id="box">
         <p>This is a div. This is a div. This is a div.</p>
         <p>This is a div. This is a div. This is a div.</p>
         <p>This is a div. This is a div. This is a div.</p>
         <p>This is a div. This is a div. This is a div.</p>
         <p>This is a div. This is a div. This is a div.</p>
      </div>
      <br>
      <script>
         function display() {
            document.getElementById("box").style.minHeight = "400px";
         }
      </script>
   </body>
</html>

更新於: 2020 年 6 月 23 日

548 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.