How to print text from textarea?

耗尽温柔 提交于 2020-03-18 05:50:08

问题


I want to print text from text area.

I have a textarea which text can be updated by user. When user update text from textarea and then print the updated text can be print on page. And this text can be print on print page without textarea.

Please suggest any solution.

Thanks


回答1:


I think I got what you are asking for. Give it a try:

<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>

Basically this will open another child window and execute javascript print on that so that the textarea and other things don't get printed.



来源:https://stackoverflow.com/questions/4732795/how-to-print-text-from-textarea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!