js实现网页中的快捷键

将下面代码插入网页body区域中,可以实现网页中快捷键功能。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<script type="text/javascript">

var userAgent = navigator.userAgent.toLowerCase();

var is_opera = userAgent.indexOf(&#039;opera&#039;) != -1 && opera.version();

var is_moz = (navigator.product == &#039;Gecko&#039;) && userAgent.substr(userAgent.indexOf(&#039;firefox&#039;) + 8, 3);

var is_ie = (userAgent.indexOf(&#039;msie&#039;) != -1 && !is_opera) && userAgent.substr(userAgent.indexOf(&#039;msie&#039;) + 5, 3);

document.onkeyup = function(e){

e = e ? e : window.event;

var tagname = is_ie ? e.srcElement.tagName : e.target.tagName;

if(tagname == &#039;INPUT&#039; &#124;&#124; tagname == &#039;TEXTAREA&#039;) return;

actualCode = e.keyCode ? e.keyCode : e.charCode;

if(actualCode == 39) {

//39为ascii码,这里是要执行的函数

}

if(actualCode == 37) {

//执行函数

}

}

</script>
updatedupdated2024-06-102024-06-10