利用Jquery实现键盘按键监听.docxVIP

  • 4
  • 0
  • 约6.02千字
  • 约 7页
  • 2016-11-26 发布于重庆
  • 举报
利用Jquery实现键盘按键监听

Js代码 ?//?shows?a?given?element?and?hides?all?othersfunction?showViaKeypress(element_id){?$(.container).css(display,none);?$(element_id).slideDown(slow);}?//?shows?proper?DIV?depending?on?link?hreffunction?showViaLink(array){?array.each(function(i)?{?$(this).click(function()?{?var?target?=?$(this).attr(href);?$(.container).css(display,none);?$(target).slideDown(slow);?});?});} ?而对键盘按键的监听是用的keypress()方法,其实也没什么难度,不过我们很少在页面上使用按键监听,这个例子比较新奇,值得我们参考,如有必要时,可以在项目里用用。 Js代码 ?$(document).keypress(function(e)?{?switch(e.which)?{?//?user?presses?the?a?case?97:????showViaKeypress(#home);?break;??//?user?presses?the?s?key?case?115:???showViaKeypress(#about);?break;??//?user?presses?the?d?key?case?100:???showViaKeypress(#contact);?break;??//?user?presses?the?f?key?case?102:???showViaKeypress(#awards);?break;??//?user?presses?the?g?key?case?103:???showViaKeypress(#links);?}?});??/seapub/archive/2012/06/05/2537214.htmlJquery 监视按键,按下回车键触发某方法 script type=text/javascript$(function () { $(input:text:first).focus(); //把焦点放在第一个文本框 var $inp = $(input); //所有的input元素$inp.keypress(function (e) { //这里给function一个事件参数命名为e,叫event也行,随意的,e就是IE窗口发生的事件。 var key = e.which; //e.which是按键的值 if (key == 13) { alert(aaa); }}); }); 一个奇怪现象: 我拖了一个Login控件,然后给它转化为template,用于自定义开发。 下 面的代码中,我捕捉到了文本栏的回车按键,这时我要去触发LoginButton来提交登录信息进行验证,可是我使用$( [id$=LoginButton]).click();只在firefox上有效,在IE无效,歪打正着的尝试一下$( [id$=LoginButton]).focus();? ,这个可以在IE上起效,在IE上focus()即完成了对焦又执行了点击,不解啊。呵呵~?$inp.keypress(function (event) { var key = event.which; if (key == 13) { $([id$=LoginButton]).click(); //支持firefox,IE武校 //$(input:last).focus(); $([id$=LoginButton]).focus();? //支持IE,firefox无效。//以上两句实现既支持IE也支持 firefox }}); ?如何防止回车(enter)键提交表单 (所有按键对应的值)?如何防止回车(enter)键提交表单,其实很简单,就一句话。onkeydown= if(event.keyCode==13)return?? false;把这句写在from标签里面就好了。下面介绍下javascript中event.keycode。keycode 8 = BackSpace BackSpace keycode 9 = Tab Tab keycode 12 = Clear keycode 13 = Enter keycode 16 = Shift_L keycode 17 = Control_L keycode 18 = Alt_L keycode 19 = Pause keyc

文档评论(0)

1亿VIP精品文档

相关文档