六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 127|回复: 0

js中回车事件的捕捉

[复制链接]

升级  15%

69

主题

69

主题

69

主题

举人

Rank: 3Rank: 3

积分
245
 楼主| 发表于 2013-2-7 20:29:45 | 显示全部楼层 |阅读模式
下面给一个例子,回车触发按钮的onclick事件。
<html>
<head><title>keydown</title></head>
<script>
function strdown(event){
<div class="articalContent  ">      if(event.keyCode==13){
           //event.keyCode=0;   //取消回车
           document.getElementByIdx("str").click();
           return false;
       }
}
function test(){
   alert("ok");
}

</script>

<bodyonkeydown="strdown(event)">
<input type="button" id="str" value="button"/>
</body>
</html>
再来看一个用jquery写的例子。
<html>
<head><title>keydown</title>
<script type='text/javascript'src='jquery-1.3.1.min.js'></script>
</head>
<script>
$(document).ready(function(){
   $(document).keydown(function(event){ //这里如果写成$(window),在ie下面就不会起作用
    if(event.keyCode==13){
               document.getElementByIdx("str").onclick();
               return false;
       }
    });
});
function test(){
    var s =document.getElementByIdx("a").value;
   alert(s);
}
</script>
<body >
<input type="text" id="a" />
<input type="button" id="str" value="button"/>
</body>
</html>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表