<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<script>var ha = document.getElementById("haha"); if(window.addEventListener)
 {
ha.addEventListener("click",function(){alert("haha");});
}else{
ha.attchEvent("onclick",function(){alert("haha");})
}
</script>
<body>
<input type="button" id="haha" value="haha" />
</body>
</html>
我是完全的新手- -!!
我在网看了JS添加事件,试做一下它
如上面的:
完全没有反应。
请各位帮一下。
看还要哪些代码,还是错了哪些代码。谢谢

解决方案 »

  1.   

    注意加载顺序,把script代码移至id='haha'的节点之后。
      

  2.   

    页面是顺序加再的,JS要放在input框的后面
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
    </head> <body> 
    <input type="button" id="haha" value="haha" /> 
    <script> 
    var ha = document.getElementById("haha"); 
    if(window.addEventListener) 

    ha.addEventListener("click",function(){alert("haha");}); 
    }else{ 
    ha.attachEvent("onclick",function(){alert("haha");}) 

    </script> 
    </body> 
    </html>
      

  3.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
        </head>
        <script>
            window.onload=function(){
                var ha = document.getElementById("haha");            if(window.addEventListener)
                {
                    ha.addEventListener("click",function(){alert("haha");});
                }else{
                    ha.attachEvent("onclick",function(){alert("haha");})
                }
            }       
        </script>
        <body>
            <input type="button" id="haha" value="haha" />
        </body>
    </html> 
      

  4.   

    1,加载顺序,所以放 onload里比较好2 , attachEvent 写错
      

  5.   

    有两点问题
    1、js的位置不对。
    把<script></script>中的代码放到button之后。给对象注册事件,首先对象必须已经加载进来了。2、ha.attchEvent("onclick",function(){alert("haha");}) 
    attchEvent拼错了,改为attachEvent
      

  6.   

    attchEvent拼错了,改为attachEvent
    window.onload=function()
    {}
      

  7.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    </head><body>
    <input type="button" name ="haha" id="haha" value="haha" />
    </body>
    </html> 
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    var ha = window.document.getElementById("haha");
        //alert(ha);null
    //if(typeof (ha) =="object")alert("ffff");
    if(window.addEventListener)
    {
    ha.addEventListener("click",function(){alert("haha");},false);
    }else{
    ha.attachEvent("onclick",function(){alert("haha");})
    }
    -->
    </SCRIPT>
    错误三点:
    1:顺序,可以放到onload里或者放到后面,加载时
    2:在ie内核里是用
    //object.attachEvent(event,function);
    在fifefox里用
    //element.addEventListener(type,listener,useCapture);