jQuery - 键入'home'做某事(keyup)

时间:2012-03-21 13:05:38

标签: jquery bind keycode keyup

所以,当我在我的网站上并输入'home'时,我想做点什么。我认为这可以使用keyup完成并可能绑定吗?

H-72 邻 - 79 M-77 E-69

我已设法做到但我确信有更好的方法......

if (e.keyCode == 72) {
    $(document).keyup(function (u) {
        if (u.keyCode == 79) {
            $(document).keyup(function (l) {
                if (l.keyCode == 77) {...

4 个答案:

答案 0 :(得分:1)

var reading = "";
$(window).keypress(function(e){
    reading += String.fromCharCode(e.which);
    if("home".indexOf(reading) < 0)
        reading = "";
    else if(reading == "home"){
        //DO WHAT YOU WANT
    }
}

答案 1 :(得分:0)

你应该做的是保持按下最后n个键的缓冲区。然后你要做的就是检查字符串中是否出现“home”字样。我会让你理清细节,但基本结构不应该太难。

//Example of checking if home is contained within "myString"
myString.indexOf("home") != -1;

var keyBuffer;
$(document).keyup(function(u){

    //Keep a key buffer.
    keyBuffer += u.keyCode;

    //Trim keybuffer so it isn't too long.

    //Check for the presence of any of your keywords such as "home" and respond.
});

答案 2 :(得分:0)

你可以试试这个

​$(function(){
    $(document).keyup(function(e){
        e.preventDefault();
        var str=$(e.target).val();
        if(str.toLowerCase()=='home')
        {
            alert("Got home here !");
        }      
    });
});​

小提琴是here

答案 3 :(得分:0)

基于jQuery.com上的Konami Code实现

if ( window.addEventListener ) {
 var hkeys = [], hhome = "72,79,77,69";
window.addEventListener("keydown", function(e){
hkeys.push( e.keyCode );

if ( hkeys.toString().indexOf( hhome ) >= 0 )
 alert("Home!!!");
 hkeys = [];
}, true);
}