目前我完成的好像是在不同的div中是实现不了的,因为我使用了nextElementSibling这个属性,如果你们有更好的全局实现的方法,不妨留在评论。
<div class="data">
<input
class="input"
placeholder="输入xxx"
/>
<input
v-for="i in arr"
:key="i"
class="input"
:placeholder="i"
/>
</div>
.input {
flex-wrap: wrap;
margin-bottom: 0.5rem;
width: 15rem;
height: 2rem;
}
const arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
// 监听键盘事件 如果按键是回车键就执行函数 handleKeyup
document.onkeydown = function (e) {
if (e.key === 'Enter') {
handleKeyup(e.key)
}
}
const handleKeyup = (e) => {
if (e === 'Enter') {
// activeElement属性获取当前获得焦点的元素
const activeInput = document.activeElement
// console.log(activeInput)
// nextSibling 属性返回元素节点之后的兄弟节点(包括文本节点、注释节点);
// nextElementSibling属性返回指定元素之后的下一个兄弟元素(相同节点树层中的下一个元素节点)(不包括文本节点、注释节点)
let nextInput = activeInput.nextElementSibling
// console.log(nextInput)
nextInput && nextInput.focus()
}
}