按下按钮时在“文本区域”框中输出文本

时间:2020-05-27 16:30:50

标签: html

我只是想要一个按钮,当按下该按钮时,它将在TextArea框内输出任何文本。像这样:

<p id="stuff"><textarea name="" id="txt" cols="20" rows="5"></textarea></p>

<script>
function myFunction() {
  document.getElementById("stuff").innerHTML = "Hello World 123";
}
</script>

(附言。我知道上面的代码无法按预期工作,这只是一个示例)

1 个答案:

答案 0 :(得分:0)

好的。看看您的代码示例,发现:

<p id="stuff"><textarea name="" id="txt" cols="20" rows="5"></textarea></p>

该段落由id="stuff"标识,而文本区域为id="text"',这就是为什么您的代码可以执行但不执行您想要的操作的原因。

以下是我要解释的有效示例:

function changeText() {
  document.getElementById('myTextArea').innerText = 'What ever I want to write in it.';
}
<textarea id="myTextArea"></textarea>
<br>
<button type="button" onclick="changeText();">Change text area text</button>

相关问题