输入框功能不起作用

时间:2014-07-30 15:10:34

标签: javascript html function input

我是这里的新手和学习编码,所以请耐心等待... 我试图制作一个代码,从输入框中获取文本并将其写为h1标题。但代码不起作用。请告诉我什么是错的......

<!Doctype html>
<html>
<head>
</head>
<body>
<input type="text" id="txtJob" value="software engineer">
<button type="button" onclick="myFunction()">HERE</button>
<script>
function myFunction(){
var id=document.getElementById("txtJob").value;
document.write("<h1>"+id+"</h1>);
}
</script>
</body>
</html>

3 个答案:

答案 0 :(得分:5)

这一行

document.write("<h1>"+id+"</h1>);

缺少"

document.write("<h1>"+id+"</h1>"); //this will work

答案 1 :(得分:4)

阅读错误控制台。

  

未捕获的SyntaxError:意外的令牌ILLEGAL(加上行号)

您忘记了第二个字符串文字的结束语

document.write("<h1>"+id+"</h1>);
//                             ^

答案 2 :(得分:0)

下面的javascript应该有效。你忘了双引号。

 window.document.write("<h1>"+window.document.getElementById("txtJob").value+"</h1>");

你想使用一个好工具,下载firefox扩展名firebug。它会向您显示代码中的错误,并且还具有控制台输出。它还允许您隔离错误。此外,在Google Chrome中,您可以使用一些扩展程序,默认情况下也是如此。