简体   繁体   English

显示包含打印样式的网页

[英]Display a webpage with styles for printing

Is there a way to display a webpage with CSS for printing instead of standard CSS? 有没有办法显示带有CSS的网页而不是标准的CSS?

I want to see how the webpage will look like before printing. 我希望在打印之前看看网页的外观。 I don't want print preview in print dialog, but the webpage with CSS for printing. 我不希望在打印对话框中打印预览,而是使用CSS进行打印的网页。 I use @media print to specify printing styles. 我使用@media print来指定打印样式。

I am not sure if you just want to see the preview while developing or to show the Vistor an Preview. 我不确定你是否只想在开发时看到预览或向Vistor展示预览。

If you want to offer your visitors an option to preview the Page before Print, i would suggest to use a JQuery Plugin like this: 如果您想为访问者提供在打印前预览页面的选项,我建议使用如下的JQuery插件:

JQuery Print Preview JQuery打印预览

如果您正在使用镀铬按F12 - >来源 - >仿真 - >媒体并检查“CSS媒体”

I understand you want the browser to show the page with the CSS for the print media. 我知道你希望浏览器显示带有CSS的打印媒体页面。

Most browsers can do that in the "print preview" (Opera does it well without opening a new window!). 大多数浏览器都可以在“打印预览”中执行此操作(Opera无需打开新窗口即​​可完成!)。

Some browsers can let the user switch between CSS when they are named; 有些浏览器可以让用户在命名时在CSS之间切换; there is a nice article on a list apart . 列表上有一篇很好的文章。 Example: 例:

<link rel="alternate stylesheet" type="text/css" href="paul.css" title="wacky" />

With this you could do something in the like: 有了这个,你可以做类似的事情:

<!-- specify the CSS for display devices -->
<link rel="stylesheet" type="text/css" href="main.css" title="default" media="display">
<!-- set an *alternate* stylesheet, the user will be able to switch between it and the previous, for display only -->
<link rel="alternate stylesheet" type="text/css" href="print.css" title="print preview" media="display">
<!-- this is the stylesheet for printing, note this is the same file as before -->
<link rel="alternate stylesheet" type="text/css" href="print.css" media="print">

And follow guidance of the previously cited article on A list appart to let the user switch between CSS. 并按照之前在A list appart上引用的文章的指导,让用户在CSS之间切换。

I don't want to include all their code, but the main part is: 我不想包含他们的所有代码,但主要部分是:

function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")<i>); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM