用JavaScript擷取輸入的特殊字元

來源:互聯網
上載者:User

Web開發時可能經常會用到在keyPress時獲得輸入的字元。String.fromCharCode()可以來實現這個,不過這個函數在你使用Shift鍵來輸入特殊字元時就不管用了,需要改造一下,這個文章給出了改造的方法,經測試在各種主流瀏覽器均正常。

<html><head><title >mapKeyPressToActualCharacter test</title><script language="javascript" type="text/javascript">    function onKeyPress() {         var e = window.event || e;        var keyCode = e.keyCode;         if (keyCode == 8) {            this.criteria.value = this.criteria.value.substring(                    0, this.criteria.value.length - 1);        }        else {            this.criteria.value                += mapKeyPressToActualCharacter(event.shiftKey, keyCode);        }     }     function mapKeyPressToActualCharacter(isShiftKey, characterCode) {        if (characterCode === 27            || characterCode === 8            || characterCode === 9            || characterCode === 20            || characterCode === 16            || characterCode === 17            || characterCode === 91            || characterCode === 13            || characterCode === 92            || characterCode === 18) {            return false;        }        if (typeof isShiftKey != "boolean" || typeof characterCode != "number") {            return false;        }        var characterMap = [];        characterMap[192] = "~";        characterMap[49] = "!";        characterMap[50] = "@";        characterMap[51] = "#";        characterMap[52] = "$";        characterMap[53] = "%";        characterMap[54] = "^";        characterMap[55] = "&";        characterMap[56] = "*";        characterMap[57] = "(";        characterMap[48] = ")";        characterMap[109] = "_";        characterMap[107] = "+";        characterMap[219] = "{";        characterMap[221] = "}";        characterMap[220] = "|";        characterMap[59] = ":";        characterMap[222] = "\"";        characterMap[188] = "<";        characterMap[190] = ">";        characterMap[191] = "?";        characterMap[32] = " ";        var character = "";        if (isShiftKey) {            if (characterCode >= 65 && characterCode <= 90) {                character = String.fromCharCode(characterCode);            } else {                character = characterMap[characterCode];            }        } else {            if (characterCode >= 65 && characterCode <= 90) {                character = String.fromCharCode(characterCode).toLowerCase();            } else {                character = String.fromCharCode(characterCode);            }        }        return character;    } </script> </head><body>    <input name="test" type="text" onkeypress="onKeyPress();"/></body></html>

 

測試結果,在Firefox, Chrome and IE.和Safari下均通過:

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.