2012-05-07 19 views
2

對於知道此事的人來說這似乎很簡單:該腳本在單擊按鈕時獲取DIV標記,並打開包含內容的新頁面。但我希望它在用戶鍵入內容後顯示textarea的內容。getElementById不會獲得textarea內容

任何想法?代碼:

<script type="text/javascript"> 
 
    var win=null; 
 
    function printIt(printThis) 
 
    { 
 
    win = window.open(); 
 
    self.focus(); 
 
    win.document.open(); 
 
    win.document.write('<'+'html'+'><'+'head'+'><'+'style'+'>'); 
 
    win.document.write('body, td { font-family: Verdana; font-size: 10pt;}'); 
 
    win.document.write('<'+'/'+'style'+'><'+'/'+'head'+'><'+'body'+'>'); 
 
    win.document.write(printThis.innerHTML); 
 
    win.document.write('<'+'/'+'body'+'><'+'/'+'html'+'>'); 
 
    win.document.close(); 
 
    win.print(); 
 
    } 
 
</script> 
 
</head><body> 
 
    
 
    <div id="activity5"> 
 
    <blockquote> 
 
     <table border="0" cellpadding="3"> 
 
     </table> 
 
     <form id="form4" name="form1" method="post" action=""> 
 
     <textarea name="Activity1a4" cols="80" rows="15" class="s23" id="ta1"></textarea> 
 
     <p> 
 
     <input name="print4" type="submit" id="print4" value="Print" onclick="printIt(document.getElementById('activity5')); return false"/> 
 
      * Retain this learning activity as part of your portfolio of evidence.</p> 
 
     <p>Check your findings with the correct answer in the back of this Learner Resource under 'Learning activity answers'.</p> 
 
     </form> 
 
    </blockquote> 
 
    </div> 
 
</body>

回答

2

你得到一個文本的內容與.value,不與.innerHTML

這意味着獲取activity5 innerHTML將不包括已輸入到子textarea中的文本。如果您想要包含該文本,則必須在textarea上分別檢索文本.value,並將其插入新頁面中的新文本區域。

+0

它不是'textarea',它是'div'。 – Guffa

+0

@Guffa - 我在答覆中添加了更多內容。基本問題是用戶在textarea中鍵入的文本不包含在父對象的innerHTML中。他們將不得不手動獲取該文本並將其插入他們想要的地方。 – jfriend00

2

您需要分別使用value屬性獲取textarea的內容,然後以相同的方式明確設置它。看看http://jsfiddle.net/EPvWV/的例子,瞭解如何做到這一點。