如何从文本区域打印文本?

5

我想从文本区域中打印文本。

我有一个文本区域,用户可以更新其中的文本。当用户从文本区域更新文本后,可以将更新后的文本打印在页面上。而且这个文本可以在没有文本区域的情况下打印出来。

请提供任何解决方案。

谢谢

1个回答

21

我认为我明白你的要求了。试试这个:

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

基本上,这将打开另一个子窗口并在其中执行 JavaScript 打印操作,以便文本区域和其他内容不会被打印出来。


顺便提一下,我给你的问题添加了另一个标签“javascript”。 - intellidiot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接