【问题标题】:Is there a way to print directly from Chrome?有没有办法直接从 Chrome 打印?
【发布时间】:2015-10-18 10:37:34
【问题描述】:

我找到了这个选择器:

document.querySelector('#print-header > div > button.print.default').click();

这个选择器有没有直接从 Chrome 打印的方法?

我也尝试过超时,但不起作用。

【问题讨论】:

  • 看来“直接”的意思是没有确认。
  • 是的。我尝试了 --kiosk-printing 模式,但不工作
  • 这只是一个按钮的选择器。您还没有向我们展示是否有任何事件侦听器(即单击)绑定到将触发打印的该元素。否则你可以简单地使用window.print()
  • 等等! “是的,[未经确认]”。这应该是不可能的。想想使用访问者的打印机打印任何他们想要的页面......
  • 是的,但我需要它来实现特殊功能。

标签: javascript google-chrome printing


【解决方案1】:

不能在没有用户确认的情况下打印。 这是一个安全问题,您不能从浏览器控制用户 PC。

当您在 chrome 中打开打印对话框时,它就像一个新选项卡,您无法控制那里。

【讨论】:

    【解决方案2】:

    (此方案需要用户确认)

    使用window.print(); 函数或查看此答案:Bill Paetzke's answser of Print the contents of a DIV

    function PrintElem(elem)
    {
        Popup($(elem).html());
    }
    
    function Popup(data) 
    {
        var mywindow = window.open('', 'my div', 'height=400,width=600');
        mywindow.document.write('<html><head><title>my div</title>');
        /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
        mywindow.document.write('</head><body >');
        mywindow.document.write(data);
        mywindow.document.write('</body></html>');
    
        mywindow.document.close(); // necessary for IE >= 10
        mywindow.focus(); // necessary for IE >= 10
    
        mywindow.print();
        mywindow.close();
    
        return true;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-26
      • 2015-03-17
      • 1970-01-01
      • 2014-04-21
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多