<html>
<head><script type="text/javascript">
 
function gg() //整个页面如果有回车键事件发生时
    
{     
 if(event.keyCode==13)     
 {     
  document.getElementById("start").click();     
  return false;  
 }     
}
</script></head><body>
<form>
<input type="button" id = "start"  onclick = "alert('sb')" value="调用函数" onkeydown = "gg()">
</form>
</body>
</html>以上这段代码按回车没反应,改成<body onkeydown = "gg()">就有了,就是把onkeydown放在body里。
之前那样调用为啥不行,不也是body里吗?求大神解释。

解决方案 »

  1.   

    <input type="button" id = "start"  onclick = "alert('sb')" value="调用函数" onkeydown = "gg()">这里的意思是必须光标在这个输入框里然后按回车才有效果<body onkeydown = "gg()">这个意思是只要本网页是当前页按回车即可。两者有区别!
      

  2.   

    是这样呀,前提是你的按钮要获得焦点,否则就是body里面的keydown事件
      

  3.   


    那我为啥加了document.getElementById("start").focus();  还是不行呢。
      

  4.   

    那我为啥加了document.getElementById("start").focus();  还是不行呢。 
      

  5.   

    刚好写完一个 希望对你有帮助    $(QueryObj).keydown(function (event) {        if (event.keyCode == 13) {            var value = $(this).val();
                var values = encodeURIComponent(encodeURIComponent($(this).val()));            window.location = PostURL + value;            //window.location = PostURL + "?Keywords=" + values;            return false;
            }    });    $(QueryObj).parent().siblings().find("input").click(function () {
            var value = $(QueryObj).val();
            var values = encodeURIComponent(encodeURIComponent($(QueryObj).val()));
            window.location = PostURL + value;        //window.location = PostURL + "?Keywords=" + values;    });
      

  6.   

        $(QueryObj).keydown(function (event) {
     
            if (event.keyCode == 13) {
     
                var value = $(this).val();
                var values = encodeURIComponent(encodeURIComponent($(this).val()));
     
                window.location = PostURL + value;
     
                //window.location = PostURL + "?Keywords=" + values;
     
                return false;
            }
     
        });
     
        $(QueryObj).parent().siblings().find("input").click(function () {
     
     
            var value = $(QueryObj).val();
            var values = encodeURIComponent(encodeURIComponent($(QueryObj).val()));
            window.location = PostURL + value;
     
            //window.location = PostURL + "?Keywords=" + values;
     
        });QueryObj 就是你的控件对象 如你的 $("#start") 就是 $() 别忘了调用 Jquery 
      

  7.   

    楼主可以结贴了 \(^o^)/~<html>
    <head>
     
    <script type="text/javascript">
    (function()
         
    {     
     if(event.keyCode==13)     
     {     
      document.getElementById("start").click();     
      return false;  
     }     
    })()
    </script>
     
    </head>
     
    <body>
    <form>
    <input type="button" id = "start"  onclick = "alert('是这样的,匿名 函数 闭包执行')" value="调用函数" >
    </form>
    </body>
    </html>
      

  8.   

    <body>
    <input type="button" id = "start"  value="回车试试" onkeydown = "gg(event)">
    <script type="text/javascript">
        window.onload=function(){
            document.getElementById("start").focus();//自动获取焦点
        };
        function gg(e){
            e = e||event;
            var code = e.which||e.keyCode;
            if (code == 13 || code == 32){
                alert("鼠标在id:start 并且你按了回车!");
            }
        }
    </script>
    </body>