htm页面:<input type="button" name="test" value="test">js:document.onclick   =   doit;   
    
  function   doit()   
  {   
   alert("OK");   
  }
我的问题是只能在button上点击的时候弹出提示框,而且不能写成这样
<input type="button" name="test" value="test" onclick="javascript:doit()">
请问如何写?

解决方案 »

  1.   

    lz用的什么浏览器?我把你代码下下来在ie和ff上都运行正常
      

  2.   

    document.getElementsByName("test")[0].onclick   =   doit;   
        
      function   doit()   
      {   
          alert("OK");   
      }
      

  3.   

    window.onload = function(){
    document.getElementsByName("test")[0].onclick = doit;
    }
    function  doit()  
      {  
          alert("OK");  
      }
      

  4.   

      我copy了lz的代码,在ie和ff中跑,只要点了页面就弹了。不是ie6
      

  5.   

    测试通过...
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>无标题文档</title>
    </head>
    <script language="javascript" type="text/javascript">
    function addAc(id)
    {
    //给对象指定函数
    var o=document.getElementById(id);
    o.onclick=doit;
    }function doit()
    {//工作函数
    alert("Test...");
    }

    </script><body>
    <input type="button" name="test" id="test" value="test">
    <script language="javascript" type="text/javascript">
    //给按钮指定函数,注意放在后面addAc("test");
    </script>
    </body>
    </html>
      

  6.   


    非常感谢,能解释一下为什么要在onload里调用吗?
      

  7.   

      原来这样,那这样改看看
    document.getElementsByName("test")[0].onclick   =   doit;   
        
      function   doit()   
      {   
          alert("OK");   
      }
      

  8.   

    给test绑定事件的时候,要求test加载完成
    不放在onload中也行,只要放在<input>后面就可以了
      

  9.   

    给test绑定事件的时候,要求test加载完成
    不放在onload中也行,只要放在<input>后面就可以了
      

  10.   

    <script language="javascript" type="text/javascript"> 
    //给按钮指定函数,注意放在后面
    addAc("test"); 
    </script> :)
      

  11.   

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
        <script type="text/javascript">
    window.onload = function(){ 
    document.getElementsByName("test")[0].onclick = doit;} 
    function  doit()  
      {  
          alert("OK");  
      }</script>
    </head>
    <body>
        <form id="form1" runat="server">
           <input type="button" name="test" value="test" />
        </form>
    </body>
    </html>