<button id='loginButton' @click='login'>登录</button>
enterButton() {
document.body.onkeydown = function (event) {
if (event.key == "Enter") {
document.getElementById("loginButton").click();
}
};
},
login(){
console.log('登录');
}
注意:enterButton方法要在created中调用
如果有多个登录按钮时可以拿一个值进行判断,在切换按钮时对logins进行重新赋值
<button id='loginButton1' @click='login'>登录</button>
<button id='loginButton2' @click='login'>登录</button>
data(){
retunr:{
logins:true
}
}
enterButton() {
var a = this;
document.body.onkeydown = function (event) {
if (event.key == "Enter") {
if(a.logins){
document.getElementById("loginButton1").click();
}else{
document.getElementById("loginButton2").click();
}
}
};
},