焦点在一个html text控件中,起初控件中没有值。当开始输入第一个字符时,触发某事件应该怎么写?(即该text控件值不为空时触发某一事件,此时焦点仍在text控件中)。

解决方案 »

  1.   

    keypress,keydown,keyup,看需要选一个就够了.推荐keydown
      

  2.   


    控件.onkeyup=function(){
       if(keycode){
           //addyourfun 
        }
    }
      

  3.   

    写错了点控件.onkeyup=function(){
       if(event.keyCode){
           //这里写你的方法
        }
    }
      

  4.   

    在if结束后还可以加个this.focus()
      

  5.   

    onkeypress就是可以实现你要的功能
      

  6.   


    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <title></title>    </head>
        <script language=JavaScript type="text/javascript">
            function keyup(){
                if(event.keyCode){
                    alert();
                }
                this.focus()
            }
            window.onload=function(){
                document.getElementById("test").onkeyup=keyup
            }
        </script>
        <body>        <div ><input type=text id=test /></div>
        </body>
    </html>推荐用keyup