onKeyUp无法使用javascript

onKeyUp无法使用javascript,javascript,html,Javascript,Html,我试图用javascript在name字段(下面的示例)中键入的内容填充div标记。我不确定我做错了什么,但它不起作用,我不知道为什么不起作用? 我想要的是当用户输入他们的名字时,它会出现在下面。我看过其他的例子,仍然不明白为什么这个例子不起作用 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4 /strict.dtd"> <html lang="en-GB">

我试图用javascript在name字段(下面的示例)中键入的内容填充div标记。我不确定我做错了什么,但它不起作用,我不知道为什么不起作用? 我想要的是当用户输入他们的名字时,它会出现在下面。我看过其他的例子,仍然不明白为什么这个例子不起作用

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4 /strict.dtd"> 
<html lang="en-GB"> 
  <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>hello whirled</title> 
    <script type="text/javascript">
        document.getElementById("name").onkeyup = function() {
            document.getElementById("display").innerHTML = this.value;
        }
    </script> 
</head> 
<body> 
    <h1> 
        Bla Bla
    </h1> 
    <form method="get" action=" "> 
        <p> 
            Other Text goes here
        </p> 
        <p> 
            Type your first name here: 
            <input type="text" id="name" value="">
        </p> 
        <div id="display">'s ball is not in the ball park.</div> 
        <noscript>  
            <div> 
                If you can see this then SCRIPTS are turned OFF on your machine and it won't work 
            </div>   
        </noscript> 
    </form> 
</body> 

你好旋风
document.getElementById(“名称”).onkeyup=function(){
document.getElementById(“display”).innerHTML=this.value;
}
布拉布拉

其他文本在这里

在此处键入您的名字:

他的球不在球场上。 如果您能看到这一点,那么您的机器上的脚本将被关闭,它将无法工作

在渲染元素之前,您正在引用该元素。这就像在做比萨饼之前先吃比萨饼

渲染元素后,需要附加事件。或者,或者在标记中的元素之后添加脚本

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4 /strict.dtd"> 
<html lang="en-GB"> 
  <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>hello whirled</title>      
</head> 
<body> 
    <h1> 
        Bla Bla
    </h1> 
    <form method="get" action=" "> 
        <p> 
            Other Text goes here
        </p> 
        <p> 
            Type your first name here: 
            <input type="text" id="name" value="">
        </p> 
        <div id="display">'s ball is not in the ball park.</div> 
        <noscript>  
            <div> 
                If you can see this then SCRIPTS are turned OFF on your machine and it won't work 
            </div>   
        </noscript> 
    </form>
    <script type="text/javascript">
        document.getElementById("name").onkeyup = function() {
            document.getElementById("display").innerHTML = this.value;
        }
    </script> 
  </body>
</html> 

你好旋风
布拉布拉

其他文本在这里

在此处键入您的名字:

他的球不在球场上。 如果您能看到这一点,那么您的机器上的脚本将被关闭,它将无法工作 document.getElementById(“名称”).onkeyup=function(){ document.getElementById(“display”).innerHTML=this.value; }
在渲染元素之前,您正在引用该元素。这就像在做比萨饼之前先吃比萨饼

渲染元素后,需要附加事件。或者,或者在标记中的元素之后添加脚本

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4 /strict.dtd"> 
<html lang="en-GB"> 
  <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>hello whirled</title>      
</head> 
<body> 
    <h1> 
        Bla Bla
    </h1> 
    <form method="get" action=" "> 
        <p> 
            Other Text goes here
        </p> 
        <p> 
            Type your first name here: 
            <input type="text" id="name" value="">
        </p> 
        <div id="display">'s ball is not in the ball park.</div> 
        <noscript>  
            <div> 
                If you can see this then SCRIPTS are turned OFF on your machine and it won't work 
            </div>   
        </noscript> 
    </form>
    <script type="text/javascript">
        document.getElementById("name").onkeyup = function() {
            document.getElementById("display").innerHTML = this.value;
        }
    </script> 
  </body>
</html> 

你好旋风
布拉布拉

其他文本在这里

在此处键入您的名字:

他的球不在球场上。 如果您能看到这一点,那么您的机器上的脚本将被关闭,它将无法工作 document.getElementById(“名称”).onkeyup=function(){ document.getElementById(“display”).innerHTML=this.value; }
Pizza示例始终是+1Ah,这就解释了这一点,因此,如果我将JavaScript放在js文件中,这会起作用吗?从我在教程中读到的内容来看,大多数人说将JavaScript放在一个单独的js文件中?我是JS的新手。因此,在我可以放置标记之后,还是会发生相同的问题,因为它是首先呈现的?是的,将它放在一个外部JS文件中,与之前的位置相同,将导致相同的错误。谢谢,有没有另一种方法可以做到这一点,不用将代码放在PHP或HTML文件的底部,而是放在一个单独的JS文件中?你可以在底部链接到一个外部文件……没有规则。比萨饼示例总是+1Ah,我明白了,这就解释了这一点,所以epascarello,如果我将JavaScript放在一个JS文件中,这会起作用吗?从我在教程中读到的内容来看,大多数人说将JavaScript放在一个单独的js文件中?我是JS的新手。因此,在我可以放置标记之后,还是会发生相同的问题,因为它是首先呈现的?是的,将它放在一个外部JS文件中,与之前的位置相同,将导致相同的错误。谢谢,有没有另一种方法可以做到这一点,而不用将代码放在PHP或HTML文件的底部,而是放在一个单独的JS文件中?你可以在底部链接到一个外部文件……没有规则。