【问题标题】:JS function to print div - Doesn't work in Chrome?用于打印 div 的 JS 函数 - 在 Chrome 中不起作用?
【发布时间】:2016-03-30 19:04:57
【问题描述】:

我正在使用以下函数来使用 JS 打印目标 Div。这在除 chrome 之外的所有浏览器中都可以正常工作。控制台报错:PrintElem is undefined。

    function PrintElem(elem) {
        Popup($(elem).html());
    }

    function Popup(data) {
        var mywindow = window.open('', 'to_print', 'height=600,width=800');
        var html = '<html><head><title></title>' +
                   '</head><body onload="window.focus(); window.print(); window.close()">' +
                   data +
                   '</body></html>';
        mywindow.document.write(html);
        mywindow.document.close();
        return true;
    }

我尝试使用以下方法调用 onclient click 函数:

PrintElement('[id$=divExport]');
PrintElement('divExport');
PrintElement('#divExport');
PrintElement('#ctl00_body_ucJobDetails_divExport');
PrintElement('ctl00_body_ucJobDetails_divExport');

它可以在 Firefox 中运行,在 IE 9 和 11 中也能正常运行!

任何建议将不胜感激。

【问题讨论】:

  • 一些 jsfiddle 样本会很好!
  • 通过看到这段代码我假设你会得到 PrintElement is undefined
  • 我认为PrintElement 是一个错字,应该是PrintElem
  • 您有什么理由不只是为此使用打印样式表...?首先会省去您打开新窗口的整个过程。

标签: javascript google-chrome printing


【解决方案1】:

您的函数名称是PrintElem()。但是您将其称为PrintElement()。除此之外,我没有看到任何其他问题..

并使用这条线。

PrintElem('#divExport');

【讨论】:

  • 如果这是真的,请关闭它,因为这是一个印刷错误。
  • 感谢您的回复...对不起,这是一个错字!是的,我确实在使用 PrintElem
  • @TAR 是不是因为这个mywindow.document.close(); 这行的目的是什么?
猜你喜欢
  • 2016-08-18
  • 2023-03-10
  • 2016-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-08
  • 2013-05-16
相关资源
最近更新 更多