首先放上效果图:
表单:
<input class="form_int" id="password" type="password" name="password" placeholder="请输入正确的密码" spellcheck="false" value=""/>
<span style="display: none;" class="pass_tip">大写锁定已打开</span>
js代码
function detectCapsLock(event){
var e = event||window.event;
var o = e.target||e.srcElement;
var oTip = o.nextSibling;
var keyCode = e.keyCode||e.which; // 按键的keyCode
var isShift = e.shiftKey ||(keyCode == 16 ) || false ; // shift键是否按住
if (
((keyCode >= 65 && keyCode <= 90 ) && !isShift) // Caps Lock 打开,且没有按住shift键
|| ((keyCode >= 97 && keyCode <= 122 ) && isShift)// Caps Lock 打开,且按住shift键
){oTip.style.display = '';}
else{oTip.style.display = 'none';}
}
document.getElementById('password').onkeypress = detectCapsLock;