本文介绍了奇怪的onkeydown行为(使用警报,不是没有)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




这是一个奇怪的问题我现在已经盯着半天了。

它涉及通过onkeydown捕捉密钥处理器。


在IE5 +中工作正常,但在Moz 1.6(和Firebird 0.7+)中,它的表现最为特殊。
奇特。 冒犯代码包含在下面。


奇怪的是警告框前面是// **。

* function textboxReplaceSelect *

如果警报处于活动状态,它可以完美运行。文本框中没有密钥重复

在屏幕上。

插入注释()即。取出警报)一个人获得两个角色

而不是一个。

它几乎看起来像''射穿''因为(制作东西

更糟糕的是)最后一个警报也向我显示了一个单一的条目。


要测试代码,请使用以下内容


TIA

Fermin DCG


< html>

< head>

< script>

var isIE = navigator.userAgent.indexOf(''MSIE'')> 1;

var isMoz = navigator.userAgent.indexOf(''Mozilla / 5。'')== 0; var _character;

var _keyCode;

var _charCode;


//解密密钥代码

函数showDown(oTextbox,evt){

_keyCode = _charCode = _character = null;

evt =(evt)? evt:((event)?event:null);


if(evt){

_keyCode = evt.keyCode;

if(evt.charCode)_charCode = evt.charCode;

else _charCode =(isIE)? _keyCode:((_ charCode)?_ charCode:_keyCode);

_character = String.fromCharCode(_charCode).toLowerCase();

}

textboxReplaceSelect(oTextbox,_character);

返回false;

}

/ **违规代码

*用一些其他文本替换当前选择的文本

* Fe:当< x>时在一个空的文本框中按下它应该只在屏幕上显示

1 x。

* @param oTextbox =用于行动的文本框

* @param sText =要插入的文本

* /

函数textboxReplaceSelect(oTextbox,sText){

if(isIE){

var oRange = document [" selection"]。createRange();

oRange.text = sText;

oRange.collapse(true );

oRange.select();

}否则if(isMoz){//这不会按预期工作

var oldText = oTextbox [" value"];

var iSelEnd = oTextbox [" selectionEnd"];

var iStart = oTextbox [" selectionStart"];

oTextbox.value ="" ;;

// ** UNCOMMENT和CODE WORKS

//alert("1.textboxReplaceSelect::" ; + oldText +" - " + sText +" - " +

oTextbox.value);

// @Textbox.value = oTextbox.value.substring (0,iStart)+ sText +

oTextbox.value.substring(oTextbox.selectionEnd,oTextbox.value.length);

oTextbox.value = oldText.substring(0,iStart)+ sText +

oldText.substring(iSelEnd,oldText.length);

//alert("2.textboxReplaceSelect::"+oTextbox.value+" - " + sText);

oTextbox.setSelectionRange(iStart + sText.length,iStart + sText.length);

}


oTextbox.focus();

alert(" exit.textboxReplaceSelect ::" + oTextbox.value +" - " + sText);

}


< / script>

< / head>

< body>

< input type =" text"值= QUOT;" ID = QUOT; txtSearch" onkeydown =" return showDown(this,

event);" />

< / body>

< / html>

Hi,

This is a strange issue I''v been staring at for half a day now.
It concerns catching keys via the onkeydown handler.

In IE5+ it works fine but in Moz 1.6 (& Firebird 0.7+) it behaves most
peculiar. The ''offensive'' code is included below.

The weird thing lies in the alert box preceded by // **.
* function textboxReplaceSelect *
If the alert is active it works perfect. No key duplication in the textbox
on the screen.
insert the comment ()i.e. take out the alert) and one gets two characters
instead of one.
It almost looks like something ''shoots through'' because (to make things
worse) the last alert shows me a singular entry as well.

To test the code just use the following

TIA
Fermin DCG

<html>
<head>
<script>
var isIE = navigator.userAgent.indexOf(''MSIE'') > 1;
var isMoz = navigator.userAgent.indexOf(''Mozilla/5.'') == 0;var _character;
var _keyCode;
var _charCode;

// decipher key down codes
function showDown(oTextbox, evt) {
_keyCode = _charCode = _character = null;
evt = (evt) ? evt : ((event) ? event : null);

if (evt) {
_keyCode = evt.keyCode;
if (evt.charCode) _charCode = evt.charCode;
else _charCode = (isIE) ? _keyCode : ((_charCode) ? _charCode : _keyCode);
_character = String.fromCharCode(_charCode).toLowerCase();
}
textboxReplaceSelect(oTextbox, _character);
return false;
}
/** THE OFFENSIVE CODE
* Replace the currently selected text with some other text
* F.e.: when a <x> is pressed in an empty textbox it should come out with
1 x only on the screen.
* @param oTextbox = the textbox to act on
* @param sText = the text to insert
*/
function textboxReplaceSelect (oTextbox, sText) {
if (isIE) {
var oRange = document["selection"].createRange();
oRange.text = sText;
oRange.collapse(true);
oRange.select();
} else if (isMoz) { // THIS DOES NOT WORK AS EXPECTED
var oldText = oTextbox["value"];
var iSelEnd = oTextbox["selectionEnd"];
var iStart = oTextbox["selectionStart"];
oTextbox.value = "";
// ** UNCOMMENT AND THE CODE WORKS
//alert("1.textboxReplaceSelect::"+oldText+" - "+sText+" - "+
oTextbox.value);
//oTextbox.value = oTextbox.value.substring(0, iStart) + sText +
oTextbox.value.substring(oTextbox.selectionEnd, oTextbox.value.length);
oTextbox.value = oldText.substring(0, iStart) + sText +
oldText.substring(iSelEnd, oldText.length);
//alert("2.textboxReplaceSelect::"+oTextbox.value+" - "+sText);
oTextbox.setSelectionRange(iStart + sText.length, iStart + sText.length);
}

oTextbox.focus();
alert("exit.textboxReplaceSelect::"+oTextbox.value +" - "+sText);
}

</script>
</head>
<body>
<input type="text" value="" id="txtSearch" onkeydown="return showDown(this,
event);" />
</body>
</html>

推荐答案



sText.length);


这应该是:

oTextbox.setSelectionRange(iStart,iStart + sText.length);

JW


sText.length);

This should be:
oTextbox.setSelectionRange(iStart, iStart + sText.length);
JW





Opera可以通过该测试。


var mightBeMoz = document.getElementById&& !document.all&& !window.opera;


但它仍然比尝试浏览器检测更好:)


var useGEBI = document.getElementById;

var useAll = document.all;

var useLayers = document.layers;


if(useGEBI){


}

else {

if(useAll){


}

否则{

if(useLayers){


}否则{

警告(''你不能这样做!'')

}

}

}


我可能在那里不匹配{}但是想法在那里:)


Ain''t物体检测盛大吗?

-

兰迪



Opera can pass that test.

var mightBeMoz = document.getElementById && !document.all && !window.opera;

But its still better than attempted browser detection :)

var useGEBI = document.getElementById;
var useAll = document.all;
var useLayers = document.layers;

if (useGEBI){

}
else{
if (useAll){

}
else{
if (useLayers){

}else{
alert(''You cant do that!'')
}
}
}

I might have mismatched { } in there, but the idea is there :)

Ain''t object detection grand?
--
Randy




IE欺骗模式中),以及Jim Ley一段时间后提到的那个(不能
记住它的名字),支持document.all和document.layers


IE spoof mode), as well as one that Jim Ley mentioned a while back (can''tremember the name of it), that supports both document.all and document.layers




你是对的,但我相信document.all代表一个函数而不是

然后属性,所以:


var isOpera = document.all&& ''function''== typeof document.all;


将在不使用window.opera属性的情况下评估Opera。

JW



You are right, but I believe that document.all represents a function rather
then a property, so:

var isOpera = document.all && ''function'' == typeof document.all;

would evaluate for Opera without using the window.opera property.
JW


这篇关于奇怪的onkeydown行为(使用警报,不是没有)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 16:10