HTML onbeforeprint 事件属性
htmldomweb developmentfront end technology更新于 2025/12/26 21:32:17
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 事件属性如何工作。

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

