HTML 参考手册

HTML 标签大全

HTML5 onbeforeprint事件属性

当相关联的文档即将打印或预览以进行打印时,将触发beforeprint事件。

HTML 事件属性

在线示例

当要打印页面时执行JavaScript :

<!DOCTYPE html>
<html>
<head>
<title>HTML onbeforeprint 事件属性的使用(菜鸟教程 (cainiaojc.com))</title>
<body onbeforeprint="myFunction()">
<h1>Try to print this document</h1>
<p><b>Tip:</b> Keyboard shortcuts, such as Ctrl+P sets the page to print.</p>
<p><b>Note:</b> The onbeforeprint event is not supported in Safari and Opera.</p>
<script>
function myFunction() {
  alert("You are about to print this document!");
}
</script>
</body>
</html>
测试看看 ‹/›

浏览器兼容性

IEFirefoxOperaChromeSafari

目前只有 Internet Explorer 和 Firefox 浏览器支持 onbeforeprint 事件属性。

定义和用法

当将要打印页面时(在出现打印对话框之前),将触发onbeforeprint属性。
提示: onbeforeprint属性通常与onafterprint属性一起使用

HTML 4.01 与 HTML5之间的差异

onbeforeprint 属性是HTML5新属性。

语法

<element onbeforeprint="script">

属性值

描述
scriptonafterprint中执行的脚本。
HTML 事件属性