javascript - 防止在密码字段中触发按键事件
问题描述
我正在编写一个脚本来捕获给定 HTML 页面中的所有按键。
现在我有一个基本的工作脚本
document.onkeypress = function(event){
var evtobj=window.event? event : e;
if (evtobj.altKey || evtobj.ctrlKey || evtobj.shiftKey)
console.log("'Alt', 'Ctrl', or 'Shift' key pressed");
console.log(String.fromCharCode(evtobj.keyCode));
}
现在,如果事件在密码字段上触发,我想阻止此函数触发。
即使事件触发了我应该如何继续识别element
是一个input
盒子并且type
是password
。
我猜应该使用event.target
.
解决方案
您也可以通过检查输入的类型来执行此操作,event.target.type
这将返回输入的类型。
<input type="password">
document.onkeypress = function(event){
if(event.target.type != 'password'){
var evtobj=window.event? event : e;
if (evtobj.altKey || evtobj.ctrlKey || evtobj.shiftKey)
console.log("'Alt', 'Ctrl', or 'Shift' key pressed");
console.log(String.fromCharCode(evtobj.keyCode));
}
}
推荐阅读
- javascript - 为什么 jquery-ui-datepicker 接受来自键盘的无效值
- swift - 从当前 UIViewController 离开时如何隐藏 tabBar?
- python - Python:从与A列中每个基因相关的一组基因中获取具有最高值的B列基因
- mysql - 更改mysql中列的类型
- android - 为什么读取内部存储中的文件需要权限?
- nginx - Traefik 实例负载均衡到 Kubernetes NodePort 服务
- mysql - JSON 更新 MySQL 表中的单个值
- ios - CORENFC 项目不能从 xcode 运行到 iphone 8
- google-sheets - 计算表格中以特定值的单元格开头的每一行的特定单词(Google表格)
- thread-safety - okapi 管道实例线程安全吗?