ES6 - Page Printing

很多时候,您希望在网页上放置一个按钮,通过实际的打印机打印该网页的内容. JavaScript帮助您使用window对象的print函数实现此功能.

JavaScript打印函数 window.print()在执行时打印当前网页.您可以使用onclick事件直接调用此函数,如以下示例所示.

示例

<html> 
   <body> 
      <form> 
         <input type = "button" value = "Print" onclick = "window.print()"/> 
      </form> 
   </body> 
</html>

成功执行上述代码后会显示以下输出.