JavaScript 30 Day -- 11 鍵盤輸入的驗證

實現效果:

完整輸入了“暗號”(一串事先定義好的字符串)時,觸發一個事件

關鍵點:

指定可激發特效的字符串
監聽並獲取輸入的字符
處理輸入,在符合條件時,觸發事件

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Key Detection</title>
  <!-- <script type="text/javascript" src="http://www.cornify.com/js/cornify.js"></script> -->
</head>
<body>
<script>
var pressed = [];
var secretCode = 'QG';
window.addEventListener('keyup',e =>{
    // console.log(e.key);
    pressed.push(e.key);
    // console.log(pressed);
    pressed.splice(- secretCode.length - 1,pressed.length - secretCode.length);
    console.log(pressed);
    if(pressed.join('').includes(secretCode)){
        console.log('8023');
    }
})
</script>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章