在
beforeprint
和afterprint
事件允许网页在打印开始前更改他们的内容(例如:可能是删除一个横幅),然后在打印完成后恢复这些更改。通常,您应该更喜欢使用@media print
CSS规则,但是在某些情况下可能有必要使用这些事件。语法
window.addEventListener("beforeprint", function(event) { ... }); window.onbeforeprint = function(event) { ... };
范例
Safari没有实现这些事件,但是您可以使用创建与beforeprint
事件等效的结果。window.matchMedia
('print')
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function(mql) {
if(mql.matches) {
console.log('webkit equivalent of onbeforeprint');
}
});
规范
规范 | 状态 | 注解 |
---|---|---|
HTML Living Standard onbeforeprint |
Living Standard |
浏览器兼容性
BCD tables only load in the browser