有趣的問題 - 它在IE發生例如,但Chrome瀏覽器。這是我在Chrome和IE中測試過的一個解決方案(它似乎有效)。
擴展版本:
腳本在頁面標題:
<script>
var buff; //Must have global scope
var input = document.getElementById("testinput"); //Defined here to save cpu (instead of on every key press).
function CheckPreBuff(e)
{
var ev = window.event ? event : e; //Get the event object for IE or FF
var unicode = (typeof(ev.keyCode) != "undefined")? ev.keyCode : ev.charCode;
if(unicode != 27) buff = input.value; //The 'escape' key has a unicode value of 27
else input.value = buff; //Only set the input contents when needed, so not to waste cpu.
}
</script>
其中 'testinput' 是我們禁用逃脫輸入。該testinput HTML低於:
<input id="testinput" onkeypress="CheckPreBuff();" onkeyup="CheckPreBuff();" type="text"/>
通知都「的onkeyup」和「onkeypress事件」中使用 - 在技術上只有「的onkeyup」是必要的,但使用「onkeypress事件」防止文本框中一片空白短暫而Escape鍵鬱悶。
支持(如果你願意)手動精縮+防錯+多重輸入
腳本在標題:
<script>
var buff = [];
function InitCPB(obj){if(typeof(obj)=="undefined")return;buff[0]=obj;buff[1]="";obj.onkeypress=CPB;obj.onkeyup=CPB;}
function CPB(e){if(typeof((buff[0]))=="undefined")return;var ev=window.event?event:e;(((typeof(ev.keyCode)!="undefined")?ev.keyCode:ev.charCode)!=27)?buff[1]=(buff[0]).value:(buff[0]).value=buff[1];}
</script>
testinput HTML標記(雖然ID不再需要):
<input onfocus="InitCPB(this);" type="text"/>
這兩種方法都保留了在按下下一個按鍵之前文本輸入內容的副本,如果按鍵按壓d是'escape',unicode 27,那麼以前的文本條目被放回到文本框中(這是歧視性的,因此腳本不會在每次按鍵時爲瀏覽器增加太多負載)。
第二個版本允許在禁用轉義鍵的同一頁面上進行多個文本輸入 - 只要它們具有如上設置的onFocus屬性(多個元素不會相互干擾)即可。它還會檢查傳遞給它的對象是否已被定義,以防止意外誤操作導致IE發生心臟病發作!
我希望這會有所幫助。
看來問題在別處。取消esc可能無濟於事。 – Mrchief
注:我注意到這個問題在Firefox 5 – philfreo
請參閱[這個答案](http://stackoverflow.com/a/10275286/2131505)。添加防止默認動作的keydown處理程序以更好的方式解決了這個問題。 – Chen