【问题标题】:Input Box functions not working [closed]输入框功能不起作用[关闭]
【发布时间】:2014-07-30 15:10:34
【问题描述】:

我是这里的新手,正在学习编码,所以请多多包涵... 我试图制作一个从输入框中获取文本并将其写为 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>

【问题讨论】:

  • 你没有在上关闭'"'
  • 非常感谢它的工作!!!非常感谢大家的回答!

标签: javascript html function input


【解决方案1】:

这一行

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

缺少"

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

【讨论】:

    【解决方案2】:

    阅读您的错误控制台。

    Uncaught SyntaxError: Unexpected token ILLEGAL (plus a line number)

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

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

    【讨论】:

    • 如果不清楚:通常可以通过在浏览器中按 F12 访问错误控制台 - 也为您提供许多其他有用的工具。
    【解决方案3】:

    下面的 javascript 应该可以工作。你忘记了双引号。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 2013-12-11
      • 2011-12-03
      相关资源
      最近更新 更多