Computer >> Máy Tính >  >> Lập trình >> HTML

Thuộc tính sự kiện onbeforeprint HTML

Thuộc tính sự kiện onbeforeprint HTML được kích hoạt khi một trang sắp được in hoặc trước khi hộp thoại in xuất hiện trong tài liệu HTML.

Cú pháp

Sau đây là cú pháp -

<tagname onbeforeprint=”script”></tagname>

Hãy để chúng tôi xem một ví dụ về Thuộc tính sự kiện onbeforeprint HTML−

Ví dụ

<!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>

Đầu ra

Thuộc tính sự kiện onbeforeprint HTML

Bây giờ, hãy thử in tài liệu bằng cách sử dụng ctrl + p và quan sát cách hoạt động của thuộc tính sự kiện onbeforeprint.

Thuộc tính sự kiện onbeforeprint HTML