Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从文本框中获取输入并更改文本_Javascript_Html - Fatal编程技术网

Javascript 如何从文本框中获取输入并更改文本

Javascript 如何从文本框中获取输入并更改文本,javascript,html,Javascript,Html,我试图从文本输入框中获取输入,将段落转换为输入框中的内容。请注意,这不是一个网页,只是在浏览器中运行的代码。 以下是到目前为止我得到的信息: <!DOCTYPE html> <html> <form><center> First name:<br> <input type="text" name="Firstname" id="JohnnyFunc"></form> <button type="bu

我试图从文本输入框中获取输入,将段落转换为输入框中的内容。请注意,这不是一个网页,只是在浏览器中运行的代码。 以下是到目前为止我得到的信息:

<!DOCTYPE html>
<html>
<form><center>
First name:<br>
  <input type="text" name="Firstname" id="JohnnyFunc"></form>
  <button type="button" onclick="myFunction">Submit</button>
<script>
  function myFunction()
{
document.getElementById("JhonnyOutput"). innerHTML="JohnnyFunc"
}
</script>
<center><p>Hello</p><p id="JhonnyOutput">Johhny!</p></center>
</html>

名字:
提交 函数myFunction() { document.getElementById(“JhonnyOutput”).innerHTML=“JohnnyFunc” } 你好,约翰


onclick属性应该在函数名后包含括号:
onclick=“myFunction()”


我可能在这里做错了什么,因为我添加了您所做的一切,但它不起作用。当我按下提交按钮时,文本从“Johnny!”变为“JohnnyFunc”。为什么?我对编码不是很有经验,所以是的。对不起,我想我没有明确说明您需要用中的第二块代码替换
myFunction()
,以便将文本从文本框输入到段落中。这里有一个工作示例
<form>
    <center>First name:<br><input type="text" name="Firstname" id="JohnnyFunc"></center>
</form>
<center><button type="button" onclick="myFunction()">Submit</button></center>
<script>
    function myFunction()
    {
        document.getElementById("JhonnyOutput"). innerHTML="JohnnyFunc"
    }
</script>
<center><p>Hello</p><p id="JhonnyOutput">Johhny!</p></center>
function myFunction() {
    var newText = document.getElementById("JohnnyFunc").value
    document.getElementById("JhonnyOutput").innerHTML = newText
}