js及vue监听键盘回车事件
js
document.onkeydown = (event) => { var e = event || window.event; if(e && e.keyCode==13){ // enter 键 function(){...} } };
vue(@keyup.enter需要当前元素处于聚焦状态,否则不生效。解决方法:给其父元素添加此事件)
<form @keyup.enter="login"> <div class="uname-box clearfloat"> <span class="icon_box user_icon"></span> <input type="text" name="" placeholder="手机号" v-model="uName" /> </div> <div class="upwd-box clearfloat"> <span class="icon_box pwd_icon"></span> <input type="password" name="" placeholder="密码" v-model="uPwd" /> </div> <div id="login" @click="login">登录</div> </form>