在 layer 弹层组件中,其确认按钮需要通过鼠标点击,而在实际需求中,我们往往想要通过回车就能够执行确定按钮事件,代码如下所示:
layer.open({
type: 1,
content: 'Where is the love?',
btn: ['确定'],
success: function(layero, index){
this.enterConfirm = function(event){
if(event.keyCode === 13){
$(".layui-layer-btn0").click();
return false; //阻止系统默认回车事件
}
};
$(document).on('keydown', this.enterConfirm); //监听键盘事件
// 点击确定按钮回调事件
$(".layui-layer-btn0").on("click",function() {
console.log("peace and love");
})
},
end: function(){
$(document).off('keydown', this.enterConfirm); //解除键盘事件
}
});
同样的,实现按 Esc 键关闭弹窗也是一样的方法,代码如下所示:
layer.open({
type: 1,
content: 'Where is the love?',
btn: ['确定'],
success: function(layero, index){
this.escQuit = function(event){
if(event.keyCode === 0x1B){
layer.close(index);
console.log("peace and love");
return false; //阻止系统默认回车事件
}
};
$(document).on('keydown', this.escQuit); //监听键盘事件
},
end: function(){
$(document).off('keydown', this.escQuit); //解除键盘事件
}
});
两张运行结果图都是一样,看不出来有什么区别,还是建议大家亲自去试一试
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有