(event.keyCode == 8)BACKSPACE问题 [英] (event.keyCode==8) BACKSPACE problems

查看:70
本文介绍了(event.keyCode == 8)BACKSPACE问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用if(event.keyCode == 8)是否有任何问题?我理解

代表退格,但它不起作用。我使用典型的键盘运行Windows XP,
- 但在检测退格时没有运气。任何人

都有这方面的经验吗?

解决方案

Julia Briggs于2005年11月21日在comp.lang上写道.javascript

是否有使用if(event.keyCode == 8)的陷阱?我知道
代表退格,但它不起作用。我正在运行Windows XP,使用典型的键盘 - 但在检测退格时没有运气。任何人都有这方面的经验?




除非< input>字段是焦点,

BS是一页保留键。


-

Evertjan。

荷兰。

(用我的电子邮件地址替换所有带点的十字架)


啊!我(想)我跟着你。当我说退格时,我实际上

表示删除键。如何在下面的代码段中替换为
检测单个删除然后dosomething()。我真的很感激你的时间。


%Julia%

< script type =''text / javascript' '>


var n = 0

函数x(){

if(event.keyCode == 35){

n + = 1

if(n == 2)alert(''2 x#'')

}


}

< / script>


< input onkeypress =''x()''>


Julia Briggs写道:

啊!我(想)我跟着你。当我说退格时,我实际上意味着删除键。你将如何替换下面的代码片段来检测单个删除然后dosomething()。我真的很感激你的时间。


这将帮助您计算出keyCodes(它还可以在更多的

浏览器中使用,而不仅仅是IE):


< script type =" text / javascript">

function sayKeyCode(event,v){

document.getElementById(String(event.type))。 innerH TML =

''值''+ event.type +'':''+ v

+''KeyCode:''+ event.keyCode; < br $>
}

< / script>


< input type =" text" size =" 10"

onkeyup =" sayKeyCode(event,this.value);"

onkeydown =" sayKeyCode(event,this.value); ">< br>

< span id =" keydown">< / span>< br>

< span id =" ; keyup">< / span>


''删除''是keyCode 46.


如果要截取密钥在任何事情发生之前,使用

onkeydown。如果你想在键完成后做某事

无论如何,请使用onkeyup。


[...]
功能x (){


需要捕获事件(见下文):


函数x(事件){

if(event.keyCode == 35){
n + = 1
if(n == 2)alert(''2 x#'')
}
[.. 。]
<输入onkeypress =''x()''>




如果你没有通过''event'',它在Firefox中不起作用


< input onkeypress =''x(事件)''


传递''事件''不会影响IE,它只是增加对支持替代Mozilla事件模型的浏览器的支持。

-

Rob


Are there any gotchas using if (event.keyCode==8)? I understand that
to represent backspace, but it doesn''t work. I am running Windows XP,
using a typical keyboard - but no luck in detecting backspaces. Anyone
have experiences with this?

解决方案

Julia Briggs wrote on 21 mrt 2005 in comp.lang.javascript:

Are there any gotchas using if (event.keyCode==8)? I understand that
to represent backspace, but it doesn''t work. I am running Windows XP,
using a typical keyboard - but no luck in detecting backspaces. Anyone
have experiences with this?



Unless an <input> field is in focus,
BS is a reserved key for one page back.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)


Ah! I (think) I am following you. When I say backspace, I actually
mean the delete key. How would you replace in your snippet below to
detect a single delete and then dosomething(). I *really* appreciate
your time.

%Julia%
<script type=''text/javascript''>

var n=0
function x(){
if(event.keyCode==35){
n+=1
if (n==2)alert(''2 x #'')
}

}
</script>

<input onkeypress=''x()''>


Julia Briggs wrote:

Ah! I (think) I am following you. When I say backspace, I actually
mean the delete key. How would you replace in your snippet below to
detect a single delete and then dosomething(). I *really* appreciate
your time.
This will help you work out keyCodes (it also works in more
browsers than just IE):

<script type="text/javascript">
function sayKeyCode(event, v){
document.getElementById(String(event.type)).innerH TML =
''Value on '' + event.type + '': '' + v
+ '' KeyCode: '' + event.keyCode;
}
</script>

<input type="text" size="10"
onkeyup="sayKeyCode(event,this.value);"
onkeydown="sayKeyCode(event,this.value);"><br>
<span id="keydown"></span><br>
<span id="keyup"></span>

''Delete'' is keyCode 46.

If you want to intercept the key before anything happens, use
onkeydown. If you want to do something after the key has done
whatever to the field, use onkeyup.

[...]
function x(){
Need to capture event (see below):

function x(event){
if(event.keyCode==35){
n+=1
if (n==2)alert(''2 x #'')
} [...]
<input onkeypress=''x()''>



If you don''t pass ''event'', it will not work in Firefox

<input onkeypress=''x(event)''

Passing ''event'' does not affect IE, it just adds support for
those browsers that support the alternative Mozilla event model.
--
Rob


这篇关于(event.keyCode == 8)BACKSPACE问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆