Internet Explorer 8 不会修改打印样式表中的 HTML5 标签

html5web developmentfront end technology更新于 2024/5/23 3:24:17

在创建打印样式表时,您需要各种 HTML5 标签,例如 <nav>、<article>、<div>、<section> 等,这些标签用于为要打印的网页提供不同的布局选项。

Chrome、Firefox 和 Internet Explorer 9 允许用户在其浏览器中使用此类 HTML5 标签。但是,Internet Explorer 6-8、Safari 4x 和其他较旧的浏览器不支持这些标签,因此我们无法在网页中应用这些布局选项。

那么,如何在 Internet Explorer 8 中使用这些 HTML5 标签呢?为此,您可以使用 html5.shiv。

首先,让我们了解一下打印样式表。

什么是打印样式表?

样式表是一组代码,它告诉浏览器网页的外观,例如大小、字体、布局、缩进、间距等方面。

打印样式表用于格式化网页,以便以有组织的方式打印在纸张上。
在 CSS 中,我们有 @media print,用于格式化要打印的页面。

示例

@media print { body {font-size: 12pt}; header {display: none}; footer {display: none}; margin: 2cm; width: 100%; }

如何创建打印 CSS?

在设计要打印的页面样式时,需要注意以下几点:-

定义边框和字体大小

首先,您应该为页面添加边距。

示例

<img src = "example.jpg" onload = "load()" width= "220" height= "300"> page { width: 100%; margin: 3cm; }

突出显示链接

由于链接在纸上无法点击,因此您应该突出显示它。

示例

a: link { background: transparent; font-color: blue; text- decoration: underline; }

隐藏视频和音频

由于视频和音频无法在纸张上显示,页面上会留下不合适的空白。因此,您应该隐藏视频和音频。

示例

.video { display: none; width: 0 !important; height: 0 !important; overflow: hidden !important; }

注意 − !important 是一条 CSS 规则,用于赋予某个属性更高的权重。如果您使用 !important

规则,它将覆盖该元素上该属性的所有先前样式代码。

隐藏其他不必要的元素

网页中有很多区域不需要打印。因此,您应该隐藏这些区域。这可以使用 display: none 来实现。

示例

nav, button { display: none; }

html5shiv 和 printshiv 用于基本样式设置

让我们了解 htmlshiv 和 printshiv 如何在样式表中工作。

html5shiv

html5shiv 是适用于 Internet Explorer 6 - 8、Safari 4.x 和 Firefox 3.x 的 Javascript shim,使其能够支持新的 HTML5 样式元素。

它用于在上述浏览器中使用 document.createElement 创建元素。它具有 print.shiv,允许在 IE 6-9 中打印 HTML5 元素时对其进行格式化。

注意

  • 它位于 <head> 标签内

  • 由于它是 Javascript shim,因此它编写在 <script> 标签下

示例

<html> <head> <title> Print stylesheet example </title> <script src = "/js/html5.shiv.js> </script> </head> <body> <section> <h2> This is an example </h2> <p> text to be entered in the section </p> <section> </body> </html>

printshiv

htmlshiv-printshiv.js 是一段代码,用于设置网页中 HTML5 元素的样式,这些元素将在 Internet Explorer 9 以下版本中打印。

示例

<head> < script src = "/js/html5shiv-printshiv.js"> </script> </head>

替代方法

您也可以使用 onbeforeprint 和 onafterprint 事件来实现此目的。

onbeforeprint 事件在页面即将打印时发生,即打印对话框出现之前。

onafterprint 事件在页面正在打印时发生,即打印对话框关闭之后。

语法

在 HTML 中,< element onbeforeprint = "yourCode">
在 JavaScript 中,object.onbeforeprint = function() { code} ;

示例

假设您想在打印网页之前隐藏视频,则可以编写以下代码 -

<body> <video onbeforeprint = "example"> </video> <script> function example() { document.getElementById( "video").style.display = "none"; } </script> </body>

结论

虽然 Internet Explorer、Firefox 和 Safari 的更新版本确实提供了各种功能,允许用户设置要打印的页面样式,但 Internet Explorer 6-9、Safari 4x 等早期版本不支持使用 HTML5 元素来格式化页面。为了让浏览器读取这些代码,您可以使用 JavaScript 的 html5.shiv 和 onbeforeprint 事件。


相关文章