aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/windoweventhandlers/onbeforeprint/index.html
blob: a5673c741fa683331d1f7137aefae3ad2dfc567d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
---
title: WindowEventHandlers.onbeforeprint
slug: Web/API/WindowEventHandlers/onbeforeprint
translation_of: Web/API/WindowEventHandlers/onbeforeprint
---
<div>{{ApiRef}}</div>

<div>{{domxref("WindowEventHandlers")}}<code><strong>onbeforeprint</strong></code>属性是用于处理当前窗口的{{event("beforeprint")}}事件的{{event("Event_handlers", "event handler")}}。在打开打印对话窗口之前会触发这些事件。</div>

<div></div>

<div><font><font></font></font><code>beforeprint</code><font><font></font></font>{{event("afterprint")}}<font><font>事件允许网页在打印开始前更改他们的内容(例如:可能是删除一个横幅),然后在打印完成后恢复这些更改。通常,您应该更喜欢使用</font></font><code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries#Media_types">@media print</a></code><font><font>CSS规则,但是在某些情况下可能有必要使用这些事件。</font></font></div>

<h2 id="语法">语法</h2>

<pre class="syntaxbox notranslate">window.addEventListener("beforeprint", function(event) { ... });
window.onbeforeprint = function(event) { ... };
</pre>

<h2 id="范例">范例</h2>

<p><font><font>Safari没有实现这些事件,但是您可以使用创建与</font></font><code>beforeprint</code><font><font>事件</font><font>等效的结果</font><font></font></font>{{domxref("window.matchMedia")}}<code>('print')</code></p>

<pre class="brush: js notranslate">var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function(mql) {
  if(mql.matches) {
    console.log('webkit equivalent of onbeforeprint');
  }
});</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">注解</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('HTML WHATWG', '#handler-window-onbeforeprint', 'onbeforeprint')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("api.WindowEventHandlers.onbeforeprint")}}</p>

<h2 id="参阅">参阅</h2>

<ul>
 <li>{{domxref("Window.print()")}}</li>
 <li>{{domxref("WindowEventHandlers.onafterprint")}}</li>
 <li><a href="/en-US/docs/Web/Guide/Printing">Printing</a></li>
</ul>