IE7& IE8 javascript打印功能

时间:2012-01-26 19:59:38

标签: javascript internet-explorer-8 internet-explorer-7

我有一个我无法弄清楚的JavaScript问题。我已经获得了一些代码片段 here我正在this页面中使用它。

这个想法是用户可以点击“打印列表”按钮,列表被复制到隐藏的iframe中的div并打印。打印页面包含iframe源HTML,列表已正确插入。但是,在IE7& 8,打印页面是完整的父页面,而不是iframe。 IE9,Chrome和FF中的行为是正确的。

我尝试调试脚本,但我看不出它出错的地方。

以下是打印列表单击触发的代码:

function printSection(id) {
  if (document.getElementById('print_frame').contentDocument){
    theIframe = document.getElementById('print_frame').contentDocument;
  }
  else {
    theIframe = document.frames['print_frame'].document;
  }
  var thePrinter = theIframe.getElementById('print_section');
  var theCopy = document.getElementById(id);
  thePrinter.innerHTML = theCopy.innerHTML;
  parent.print_frame.printPage();
}

这是printPage()函数:

function printPage() {
  window.parent.print_frame.focus();
  window.print();
}

我很感激任何帮助。如果您需要更多信息,请与我们联系。非常感谢。

1 个答案:

答案 0 :(得分:1)

更简单的解决方案可能只是使用CSS媒体类型来隐藏页面内容并显示一个隐藏的元素进行打印。

CSS

.print{display:none;}
@media print {
    .pagecontainer{display:none;}
    .print{display:block;}
}

HTML

<body>
    <div class="pagecontainer">
        Page content here
    </div>
    <div class="print">Only show this when printing</div>
</body>