所以当我在我的网站上键入"home"时,我想做点什么。我认为这可以使用keyup和可能的绑定来完成?
h-72o-79m-77e-69
我设法做到了,但我相信还有更好的方法。。
if (e.keyCode == 72) {
$(document).keyup(function (u) {
if (u.keyCode == 79) {
$(document).keyup(function (l) {
if (l.keyCode == 77) {...
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
}
}
您应该做的是保留最后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.
});
基于jQuery.com 上的Konami代码实现
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);
}
你可以试试这个
$(function(){
$(document).keyup(function(e){
e.preventDefault();
var str=$(e.target).val();
if(str.toLowerCase()=='home')
{
alert("Got home here !");
}
});
});
小提琴在这儿。