目录

如何 - 检测大写锁定


了解如何使用 JavaScript 确定输入字段内的大写锁定是否打开。


检测大写锁定是否打开

尝试按输入字段内的 "Caps Lock" 键:

警告!大写锁定已打开。

示例

// Get the input field
var input = document.getElementById("myInput");

// Get the warning text
var text = document.getElementById("text");

// When the user presses any key on the keyboard, run the function
input.addEventListener("keyup", function(event) {

  // If "caps lock" is pressed, display the warning text
  if (event.getModifierState("CapsLock")) {
    text.style.display = "block";
  } else {
    text.style.display = "none"
  }
});
亲自试一试 »

提示:阅读有关 getModifierState() 方法的更多信息JavaScript 事件参考 - getModifierState()