如何处理使用 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 次浏览

启动你的 职业生涯

完成该课程并获得认证

开始行动
广告