在Firefox中取消活动 [英] Cancel Event in Firefox

查看:59
本文介绍了在Firefox中取消活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数需要取消输入到

表格的文本字段。我在IE中取消了该事件并且文本没有输入

。但是对于firefox(1.0)我取消了这个事件,文字仍然是

进入。


我已经剥离了这个功能只是为了试图获得它在

Firefox中工作。警报确实发生在keydown事件上,因此它应该是

可取消。


函数格式输入(e){

if( e.cancelable){

e.preventDefault()

提醒(事件可以取消)

}

}


如何取消输入?

解决方案

ry ****** @ yahoo.com 写道:

我有一个需要取消输入到表单上的文本字段的函数。我在IE中取消了该事件并且文本没有输入
。但是对于firefox(1.0)我取消了这个事件,文本仍然会被输入。

我已经剥离了这个功能,只是为了让它在
中运行起来。 Firefox浏览器。警报确实发生在keydown事件上,因此它应该是可取消的。

函数格式输入(e){
if(e.cancelable){
e.preventDefault ()
警告(事件可取消)
}

如何取消输入?




为什么不只是禁用文本字段,模糊它,并删除

输入的内容?


因为我不想禁用它。这是一个剥离的功能,尝试

来让它工作。我只想取消某些keyCodes上的事件

而不是所有的keyCodes。你不能删除删除,结束或家庭等。

任何人都知道如何取消事件,因此它不会继续按

键码按下?

Rob写道:

ry ****** @ yahoo。 com 写道:

我有一个函数需要取消输入到表单上的文本字段。我在IE中取消了该事件并且文本没有输入
。但是对于firefox(1.0)我取消了这个事件,文本仍然会被输入。

我已经剥离了这个功能,只是为了让它在
中运行起来。 Firefox浏览器。警报确实发生在keydown事件上,因此它应该是可取消的。

函数格式输入(e){
if(e.cancelable){
e.preventDefault ()
警告(事件可取消)
}

如何取消输入?



为什么不只是禁用文本字段,模糊它并删除
输入的内容?




ry******@yahoo.com 写道:

因为我没有想要禁用它。这是一个剥离的功能,试图让它工作。我只想取消某些keyCodes上的事件
而不是所有的keyCodes。你不能删除删除,结束或家庭等。
任何人都知道如何取消事件,以便它不会继续按
keyCode?

主要规则是:你必须从事件处理程序返回false到

取消一个事件。下面的代码适用于FF 1.0.7


请注意:

1)在内部事件处理程序(textbox txt1)中,你必须返回false

*来自处理程序本身* - 而不是来自

处理程序调用的函数。我猜这是你原来的问题。


2)当前版本的FireFox中存在一个令人讨厌的错误。在文本框中键入任何文本

会导致安全性异常(您可以查看

JavaScript控制台)。它不会破坏脚本,但它会导致执行中的小b / b
延迟。如果您通过自定义检查另外传递每个输入事件

,则此延迟可能会显着。唯一的

解决方法我知道将自动完成设置为false;但它不是

总是合适的。


< html>

< head>

< title>测试< / title>

< meta http-equiv =" Content-Type" content =" text / html;

charset = iso-8859-1">

< script type =" text / javascript">

/ *仅适用于FireFox和Gesko的脚本* /


函数f(e){

if(String.fromCharCode(e) .which)。

toUpperCase()==''A''){

返回false;

}

else {

返回true;

}

}


函数init(){

document.forms [0] .elements [''txt2'']。onkeypress = f;

}


窗口.onload = init;

< / script>

< / head>


< body>


< form method =" post" action ="">

< input type =" text"自动填充= QUOT假QUOT;名称= QUOT; TXT1" onkeypress =" return

f(event)">

< input type =" text"自动填充= QUOT假QUOT; name =" txt2">

< / form>


< / body>

< / html> ;


I have a function which needs to cancel the input into a text field on
a form. I cancel the event in IE fine and the text does not get
entered. But for firefox (1.0) I cancel the event and the text still
gets entered.

I''ve stripped the function down just to try to get it to work in
Firefox. The alert does happen on the keydown event so it should be
cancellable.

function formatInput(e){
if (e.cancelable){
e.preventDefault()
alert("Event is cancelable")
}
}

How can I cancel the input??

解决方案

ry******@yahoo.com wrote:

I have a function which needs to cancel the input into a text field on
a form. I cancel the event in IE fine and the text does not get
entered. But for firefox (1.0) I cancel the event and the text still
gets entered.

I''ve stripped the function down just to try to get it to work in
Firefox. The alert does happen on the keydown event so it should be
cancellable.

function formatInput(e){
if (e.cancelable){
e.preventDefault()
alert("Event is cancelable")
}
}

How can I cancel the input??



Why not just disable the text field, blur it, and erase whatever was
entered?


Cause I do not want to disable it. That was a stripped function to try
to get it to work. I only want to cancel the event on certain keyCodes
not ALL keyCodes. You can''t erase a delete, or an End or a Home, etc.
Anyone know how to cancel the event so it does not continue with the
keyCode pressed?
Rob wrote:

ry******@yahoo.com wrote:

I have a function which needs to cancel the input into a text field on
a form. I cancel the event in IE fine and the text does not get
entered. But for firefox (1.0) I cancel the event and the text still
gets entered.

I''ve stripped the function down just to try to get it to work in
Firefox. The alert does happen on the keydown event so it should be
cancellable.

function formatInput(e){
if (e.cancelable){
e.preventDefault()
alert("Event is cancelable")
}
}

How can I cancel the input??



Why not just disable the text field, blur it, and erase whatever was
entered?





ry******@yahoo.com wrote:

Cause I do not want to disable it. That was a stripped function to try
to get it to work. I only want to cancel the event on certain keyCodes
not ALL keyCodes. You can''t erase a delete, or an End or a Home, etc.
Anyone know how to cancel the event so it does not continue with the
keyCode pressed?



The main rule is: you have to return false from the event handler to
cancel an event. The code below works for FF 1.0.7

Please not that:
1) In intrisic event handlers (textbox txt1) you have to return false
*from the handler itself* - not from the function called by the
handler. I guess that was your original problem.

2) There is a nasty bug in current versions of FireFox. Typing any text
in a textbox leads to the security exception (you can check the
JavaScript console). It doesn''t break the script, but it causes a small
delay in the execution. If you additionally pass each input event
through a custom check, this delay may get noticable. The only
workaround I''m aware of in setting autocomplete to false; but it''s not
always suitable.

<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript">
/* Script for FireFox and Gesko-based only */

function f(e) {
if (String.fromCharCode(e.which).
toUpperCase() == ''A'') {
return false;
}
else {
return true;
}
}

function init() {
document.forms[0].elements[''txt2''].onkeypress = f;
}

window.onload = init;
</script>
</head>

<body>

<form method="post" action="">
<input type="text" autocomplete="false" name="txt1" onkeypress="return
f(event)">
<input type="text" autocomplete="false" name="txt2">
</form>

</body>
</html>


这篇关于在Firefox中取消活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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