javascript 如何从textarea打印文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4732795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:42:53  来源:igfitidea点击:

How to print text from textarea?

javascriptruby-on-railsprinting

提问by Jayashri

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.

我有一个文本区域,用户可以更新其中的文本。当用户从 textarea 更新文本然后打印更新的文本时,可以在页面上打印。并且可以在没有 textarea 的打印页面上打印此文本。

Please suggest any solution.

请提出任何解决方案。

Thanks

谢谢

回答by intellidiot

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.

基本上这将打开另一个子窗口并在其上执行 javascript 打印,以便不会打印 textarea 和其他内容。