【问题标题】:PHP stylesheet print switcher problem revisited?PHP 样式表打印切换器问题重温?
【发布时间】:2009-11-19 06:29:18
【问题描述】:

好的,我有这个样式表切换器,它只有在我从样式表链接中省略 media="print" 时才能工作。

我想知道如何在不遗漏 media="print" 属性的情况下解决此问题。

这是 PHP 代码。

<!-- Print Script -->
<?php if (isset($_GET['css']) && $_GET['css'] == 'print') { ?>
<meta name="robots" content="noindex" />
<link rel="stylesheet" type="text/css" href="http://localhost/styles/print.css" media="print" />
<script type="text/javascript">
//<![CDATA[
if(window.print())
  onload = window.print();
else
  onload = window.print;
//]]>
</script> 
<?php } else { ?>
<link rel="stylesheet" type="text/css" href="http://localhost/styles/style.css" media="screen" />
<?php } ?>
<!-- End Print Script -->

这里是您单击以更改样式表的链接。

<a href="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>?css=print" id="print-page" title="Print">Print This Page</a>

【问题讨论】:

  • "if(window.print())"... 这看起来不对。我认为应该是“if(typeof window.print == 'function')”

标签: php javascript css


【解决方案1】:

您不需要 PHP 代码来确定是否输出 CSS 文件进行打印。默认情况下,浏览器不会呈现“打印”样式表,并且在打印时应该忽略“屏幕”样式表。

可能存在一些渲染问题:浏览器可能没有足够的时间来渲染页面并将其正确馈送到打印机。

一个简化的解决方案是:

<head>
<link rel="stylesheet" type="text/css" href="http://localhost/styles/print.css" media="print" />
<link rel="stylesheet" type="text/css" href="http://localhost/styles/style.css" media="screen" />
<script type="text/javascript">
function print_it() {
if(window.print())
  onload = window.print();
else
  onload = window.print; 
}
</script>
</head>

<body>
<a href="javascript:print_it();" id="print-page" title="Print">Print This Page</a>
</body>

【讨论】:

  • 我希望用户先看到打印的页面,然后是打印对话框。
  • 在这种情况下,在适合打印的页面版本中,“print.css”的媒体应该是“print, screen”。浏览器将使用相同的样式表显示和打印
  • 另见我对“if(window.print())”语句的评论
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-08
  • 1970-01-01
  • 1970-01-01
  • 2014-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多