简体   繁体   English

javascript获取段落的值以显示在textarea中

[英]javascript get value of paragraph to appear in textarea

I'm trying to add the value of a paragraph to a text area using javascript. 我正在尝试使用javascript将段落的值添加到文本区域。 This is my code at the moment. 这是我目前的代码。 I can't quite seem to get it to work. 我似乎不太能正常工作。 Any help is appreciated 任何帮助表示赞赏

  <html>
  <head>
   </head>
  <body>
  <button value="this is a length definition" onclick="reveal(this.value)"> hi</button>
  <script type="text/javascript">
  function reveal(value)
  {
  var text = value;
  document.outputtext.value += value;
  }
  </script>
  <table>
  <tr>
  <td><textarea name="outputtext"></textarea></td>
  </tr></table>
  </body>
  </html>

Your textarea is not part of the document. 您的文本区域不是文档的一部分。 Also your button value is not a paragraph. 同样,您的按钮值也不是段落。

  • Give it an ID and use document.getElementById("outputtext").value (recommended) or 给它一个ID并使用document.getElementById("outputtext").value (推荐)或
  • wrap in a form and do document.formName.outputtext.value or 包装成表格并执行document.formName.outputtext.value
  • use the clumsy document.getElementsByName("outputtext")[0].value 使用笨拙的document.getElementsByName("outputtext")[0].value

Here is the code using ID 这是使用ID的代码

DEMO DEMO

  <html>
  <head>
   </head>
  <body>
  <button value="this is a length definition" onclick="reveal(this.value)"> hi</button>
  <script type="text/javascript">
  function reveal(value) {
    document.getElementById("outputtext").value += value;
  }
  </script>
  <table>
  <tr>
  <td><textarea id="outputtext"></textarea></td>
  </tr></table>
  </body>
  </html>

shoud change to 应该更改为

<textarea id="outputtext"></textarea>

then code change to 然后将代码更改为

document.getElementById('outputtext')

good luck 祝好运

You can use name but you have to call getElemntsByName() and receive an array of element 您可以使用name,但必须调用getElemntsByName()并接收元素数组

<html>
<head>
</head>
<body>
<button value="this is a length definition" onclick="reveal(this.value)"> hi</button>
<script type="text/javascript">
function reveal(value)
{
  var text = value;
  document.getElementById("outputtext").value += text;
}
</script>
<table>
  <tr>
    <td><textarea id="outputtext"></textarea></td>
  </tr></table>
</body>
</html>

Code change to: 代码更改为:

 <html>
 <head>
 </head>
<body>
<button value="this is a length definition" onclick="reveal(this.value);"> hi</button>
<script type="text/javascript">
function reveal(value)
     document.getElementById("outputtext").value += value;
 }
</script>
<table>
  <tr>
    <td><textarea id="outputtext"></textarea></td>
  </tr></table>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM