如何通过 JavaScript 设置元素中的分页行为?


在 JavaScript 中使用 pageBreakInside 属性来设置元素中的分页行为。使用 auto 属性在元素内部插入分页符。使用 auto avoid 属性值可根据需要在元素内部自动插入分页符,或避免分页。

注意 - 这些更改在打印或查看打印预览时可见。

示例

你可以尝试运行以下代码,以通过 JavaScript 返回元素内部的分页行为 -

<!DOCTYPE html>
<html>
   <body>
      <h1>Heading 1</h1>
      <p>This is demo text.</p>
      <p>This is demo text.</p>
      <p id = "myFooter">This if footer text.</p>
      <button type = "button" onclick="display()"> Set page-break </button>
     
      <script>
         function display() {
            document.getElementById("myFooter").style.pageBreakInside = "auto";
         }
      </script>
   </body>
</html>

更新于: 2020-06-23

2K+ 次浏览

开启你的 职业生涯

完成课程取得认证

开始学习
广告
© . All rights reserved.