getElementById 不会获取文本区域内容

getElementById doesn't get textarea content

本文关键字:区域 取文本 获取 getElementById      更新时间:2023-09-26

对于知道的人来说,这似乎很简单:单击按钮时,此脚本会获得一个DIV标记,并打开包含内容的新页面。但我希望它在用户输入内容后显示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>

您需要

使用 value 属性单独获取文本区域的内容,然后以相同的方式显式设置它。查看 http://jsfiddle.net/EPvWV/以获取有关如何执行此操作的示例。

您可以使用 .value 而不是 .innerHTML 获取文本区域的内容。

这意味着获取 innerHTML activity5将不包括在子文本区域中键入的文本。 如果要包含该文本,则必须在文本区域上单独检索带有.value的文本,并将其插入到新页面的新文本区域中。