在有些网页中我们希望用户按回车就可以提交数据或禁止按回车提交数据,下面我来到介绍一个js响应上、下键enter键程序代码.
- <script type="text/javascript" charset="utf-8">
- function response(obj){
- var $list = $(obj);
- var $children = $list.children();
- var num= $children.size();
- var current=0;
- addnow()
- $(document).bind('keydown',function(e){
- if($list.is(":visible")){
- if(e.keyCode == 40){
- if(current<num -1) current = current +1;
- addnow()
- e.preventDefault();
- }else if(e.keyCode ==38 ){
- if(current > 0)current= current -1;
- addnow()
- e.preventDefault();
- }else if(e.keyCode == 13){
- var url = $children.eq(current).find('a').attr('href');
- document.location= url;
- }
- }else{
- current = 0;
- addnow()
- }
- });
- function addnow(){
- $children.eq(current).addClass('now').siblings().removeClass('now');
- }
- }
- response('#ul');
-