例如,当用户在主页上键入键盘上的“Hello”时,会发生Javascript警报。 我需要这个使用Javascript在我的网站上实现某种复活节彩蛋。 像2009年Facebook的Konami东部蛋一样
答案 0 :(得分:1)
这是一个非常基本的例子,说明如何在jQuery的帮助下完成它:
var hello = [72, 101, 108, 108, 111].join();
$(window).on('keypress', function(e) {
var $this = $(this),
gap = e.timeStamp - ($this.data('time') || e.timeStamp),
chars = $this.data('chars') || [];
if (gap > 1000) {
chars = [];
}
chars.push(e.which);
if (chars.join() === hello) {
alert('Hello ;)');
}
$this.data('chars', chars);
$this.data('time', e.timeStamp);
});
答案 1 :(得分:1)
改进:
var seq = "rainbow"
var input = ""
window.addEventListener("keypress", function(e) {
input += String.fromCharCode(e.keyCode)
for (var i = 0; i < seq.length; i++) {
if (input[i] != seq[i] && input[i] != undefined) {
input = ""
}
}
if (input == seq) {
alert("EASTER EGG!")
input = ""
}
})
答案 2 :(得分:0)
当按键顺序&#34;你好&#34;键入并显示警报。
这是一个有效的JSfiddle。
let stringVal="";
document.onkeypress = function (e) {
e = e || window.event;
console.log(e.keyCode); // use this to debug the key codes
switch(e.keyCode){
case 104: // key code for h
stringVal+="h";
break;
case 101: // key code for e
stringVal+="e";
break;
case 108: // key code for l
stringVal+="l";
break;
case 108: // key code for l
stringVal+="l";
break;
case 111: // key code for o
stringVal+="o";
break;
default:
stringVal=""; // this step is important to reset the value when any other key is pressed
}
/* if the keys 'hello' are pressed consecutively */
if(stringVal.indexOf("hello")!=-1)
alert('');
}
答案 3 :(得分:-1)