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>

 

posted @ 2019-04-18 14:01  小白&小菜  阅读(1240)  评论(0编辑  收藏  举报
我是一个小菜鸟,飞呀飞呀,快飞高