2011-01-19 81 views
5

我想從文本區域打印文本。如何從textarea打印文本?

我有一個textarea文本可以由用戶更新。當用戶從textarea更新文本,然後打印更新的文本時,可以在頁面上打印。這個文本可以打印在沒有textarea的打印頁面上。

請提出任何解決方案。

謝謝

回答

15

我想我得到了你所要求的。試試看:

<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的打印,使textarea的和其他的東西沒有得到打印。

+0

謝謝,它適用於我。 – Jayashri 2011-01-19 10:10:32