假设report.html是您要打印的页面。开发页面的方式是它接受媒体参数作为GET参数。即像yoursite.com/report.html?media=X
- 其中X可以是'screen'
,'print'
等,如果X是空的,它可以使用“屏幕”
写2 css文件即screen.css和print.css的默认值 - 根据该值媒体参数(X)将screen.css或print.css导入您的页面。
在screen.css,写一个 '@media屏幕' 里面你的风格DEFS块,如:
@media screen {
body {
... screen style here ...
}
}
在print.css,写一个 '@media屏幕,打印' 块内等你的风格DEFS :
@media screen, print {
body {
... print style here ...
}
}
在你report.html,假设你有一个打印按钮,在其onclick
,叫window.open('report.html?media=print', ...)
。在接收到Ctrl + P时,也可以从连接到文档对象的keydown处理程序执行相同的操作。
而且,在你的页面的onload
,检查媒体的说法是“打印”,如果它,然后在短暂的延迟之后调用window.print()
(比如500毫秒),即是这样的:
if(window.location.href.indexOf('media=print')!=-1) {
setTimeout(function() { window.print(); }, 500);
}
究竟什么是你的题?你在你的问题中回答了这两个问题。 – Blender
他要求一个例子......“任何示例请”应该暗示在:) – ChrisR
@Blender我知道通过添加window.print将加载打印选项 '
' 但它不会首先显示打印预览。我该怎么做? –