如何處理使用 JavaScript 渲染在元素框外部的內容?


如果內容渲染到元素框的外部,請使用 overflow 屬性新增捲軸。這將方便訪問者閱讀全部內容。

示例

你可嘗試執行以下程式碼,瞭解如何在 JavaScript 中使用 overflow 屬性 −

即時演示

<!DOCTYPE html>
<html>
   <head>
      <style>
         #box {
            width: 450px;
            height: 150px;
            background-color: orange;
            border: 3px solid red;
            margin-left: 20px;
         }
      </style>
   </head>
   <body>
      <p>Click to use overflow property and set scroll.</p>
      <button type="button" onclick="display()">Set Scroll</button>
      <div id="box">
         <p>This is a div. This is a div. 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. This is a div. This is a div.</p>
         <p>This is a div. This is a div. 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. This is a div. This is a div.</p>
         <p>This is a div. This is a div. 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. This is a div. This is a div.</p>
      </div>
      <br>
      <br>
      <script>
         function display() {
            document.getElementById("box").style.overflow = "scroll";
         }
      </script>
   </body>
</html>

更新時間: 23-6 月-2020

208 次瀏覽

啟動你的 職業生涯

完成該課程並獲得認證

開始行動
廣告