如何从textarea打印文本?

Jay*_*hri 5 javascript printing ruby-on-rails

我想从文本区域打印文本.

我有一个textarea文本可以由用户更新.当用户从textarea更新文本然后打印更新的文本可以在页面上打印.此文本可以在没有textarea的打印页面上打印.

请建议任何解决方案.

谢谢

int*_*iot 17

我想我得到了你要求的东西.试试看:

<html>
  <head>
    <title>Print TextArea</title>
    <script type="text/javascript">
      function printTextArea() {
        childWindow = window.open('','childWindow','location=yes, menubar=yes, toolbar=yes');
        childWindow.document.open();
        childWindow.document.write('<html><head></head><body>');
        childWindow.document.write(document.getElementById('targetTextArea').value.replace(/\n/gi,'<br>'));
        childWindow.document.write('</body></html>');
        childWindow.print();
        childWindow.document.close();
        childWindow.close();
      }
    </script>
  </head>
  <body>
    <textarea rows="20" cols="50" id="targetTextArea">
      TextArea value...
    </textarea>
    <input type="button" onclick="printTextArea()" value="Print Text"/>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

基本上这将打开另一个子窗口并在其上执行javascript打印,以便textarea和其他东西不被打印.