标签:输入 === attr ber inpu typelist onkeydown lis NPU
//在生命周期中监听键盘keydown事件
const _this = this;
document.onkeydown = function(e) {
console.log(e)
if (e.keyCode === 13) {
const isFromHaveOneInput = _this._isFromHaveOneInput(e.target)
if (isFromHaveOneInput) {
e.preventDefault()
}
}
}
//判断表单中是否只有一个input 输入框
_isFromHaveOneInput(target) {
const form = target.form
const typeList = [‘password‘, ‘number‘, ‘email‘, ‘search‘, ‘tel‘, ‘text‘]
if (target.nodeName !== ‘INPUT‘) {
return false
}
if (!form) {
return false
}
const formChildInput = []
let i = 0
while (form[i]) {
if (form[i].nodeName === ‘INPUT‘ && typeList.includes(form[i].getAttribute(‘type‘))) {
formChildInput.push(form[i])
}
i++
}
return formChildInput.length === 1
}
标签:输入 === attr ber inpu typelist onkeydown lis NPU
原文地址:https://www.cnblogs.com/dxl-ggr/p/14108060.html