怎么做这个javascript我没有得到按钮的输出

时间:2015-10-28 11:52:01

标签: javascript html

有一个问题是获得输出,如果可以帮助我请帮助我

<!Doctypehtml>
<html>
<title>new html</title>
<head>
</head>
<body>
<button id="open">click me</button>
<script >
var open1=document.getElementById("open");
var n="hi";
open1.addEventListener("click", function(){
alert(n.length);
});
</script>
</body>
</html>

3 个答案:

答案 0 :(得分:2)

在事件名称之后你忘记了:

var open1=document.getElementById("open")
var n="hi";
open1.addEventListener("click", function(){
    alert(n.length);
});

答案 1 :(得分:2)

以下是您的更新代码:您在“点击”后错过了逗号(,)。

<!Doctypehtml>
<html>
<title>new html</title>
<head>
</head>
<body>
    <button id="open">click me</button>
    <script>
           var open1=document.getElementById("open")
           var n="hi";
           open1.addEventListener("click",function(){
               alert(n.length);
           });
    </script>
</body>
</html>

答案 2 :(得分:0)

<!Doctypehtml>
   <html>
       <title>new html</title>
       <head></head>
       <body>
           <button id="open">click me</button>
           <script >
           var open1=document.getElementById("open")
           var n="hi";
           open1.addEventListener("click",function(){
               alert(n.length);
           });
           </script>
      </body> 
</html>

点击

后,您错过了“,”