当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JS 实现分页打印功能

JS 实现分页打印功能

2018年05月29日  | 移动技术网IT编程  | 我要评论

在调用window.print()时,可以实现打印效果,但内容太多时要进行分页打印。

在样式中有规定几个打印的样式

page-break-beforepage-break-after css属性并不会修改网页在屏幕上的显示,这两个属性是用来控制文件的打印方式。

每个打印属性都可以设定4种设定值:auto、always、left和right。其中auto是默认值,只有在有需要时,才需设定分页符号 (page breaks)。

page-break-before若设定成always,则是在遇到特定的组件时,打印机会重新开始一个新的打印页。

page-break-before若设定成left,则会插入分页符号,直到指定的组件出现在一个左边的空白页上。

page-break-before若设定成right,则会插入分页符号,直到指定的组件出现在一个右边的空白页上。

page-break-after属性会将分页符号加在指定组件后,而非之前。

在下列程序中您将可以看到这些属性的设定,

<html>
  <head>
    <title>listing 14-4</title>
  </head>
  <body>
    <div>this is the first div.</div>
    <div style="page-break-before:always">this is the second div.</div>
    <div style="page-break-after:always">this is the third div.</div>
    <div>this is the fourth div.</div>
    <div style="page-break-before:right">this is the fifth div.</div>
    <div style="page-break-after:right">this is the sixth div.</div>
    <div>this is the last div.</div>
  </body>
</html>

描述
auto 默认值。如果必要则在元素前插入分页符
always 在元素前插入分页符
avoid 避免在元素前插入分页符
left 在元素之前足够的分页符,一直到一张空白的左页为止
right 在元素之前足够的分页符,一直到一张空白的右页为止
inherit 规定应该从父元素继承 page-break-before 属性的设置

在dom对象中pagebreakbefore属性

语法:

    object.style.pagebreakbefore=auto|always|avoid|left|right

<html>
  <head>
    <script type="text/javascript">
      function setpagebreak()
      {
        document.getelementbyid("p2").style.pagebreakbefore="always";
      }
    </script>
  </head>
  <body>
    <p>this is a test paragraph.</p>
    <input type="button" onclick="setpagebreak()" value="set page-break" />
    <p id="p2">this is also a test paragraph.</p>
  </body>
</html>

总结

以上所述是小编给大家介绍的js 实现分页打印功能,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网