HTML onbeforeprint 事件属性
HTML onbeforeprint 事件属性会在打印页面或 HTML 文档中显示打印对话框之前触发。
语法
语法如下:
<tagname onbeforeprint=”script”></tagname>
让我们来看一个 HTML onbeforeprint 事件属性的示例:
示例
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: #000;
height: 100vh;
background-color: #FBAB7E;
background-image: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%);
text-align: center;
padding: 20px;
}
p {
font-size: 1.1rem;
}
</style>
</head>
<body onbeforeprint="get()">
<h1>HTML onbeforeprint Event Attribute Demo</h1>
<p>I'm a paragraph HTML element with some dummy text.</p>
<p style="color:#db133a;">Now try to print this document.</p>
<div class="show"></div>
<script>
function get() {
document.body.style.background = "lightblue";
}
</script>
</body>
</html>输出

现在尝试使用 Ctrl + P 打印文档,并观察 onbeforeprint 事件属性如何发挥作用。

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP