在引导程序中关闭模态后,如何取消模态触发按钮的焦点 [英] How can I unfocus the modal trigger button after closing the modal in bootstrap

查看:123
本文介绍了在引导程序中关闭模态后,如何取消模态触发按钮的焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关闭模态后,我似乎无法模糊按钮.

I can't seem to blur the button after closing the modal.

$('#exampleModal').on('hidden.bs.modal', function(e){
        $('button').blur();
    });

我已经尝试了上面的方法,但它似乎并没有使它模糊.我已经尝试了几乎所有东西.唯一的解决方案是设置超时并在模型完成隐藏过渡后对其进行模糊处理.有更好的解决方案吗?

I've tried the above and it still doesn't seem to blur it. I've tried almost everything. The only solution is to set a timeout and blur it after the model finishes the hidden transition. Any better solution?

推荐答案

可以使用.one()绑定在模态插件中设置对触发器元素的关注,但遗憾的是无法绑定.好消息是我们可以做到这一点:

The focus back to the trigger element is set within the modal plugin using a .one() binding, which unfortunately cannot be unbound. The good news is we can do this:

$('#myModal').on('shown.bs.modal', function(e){
    $('#myModaltrigger').one('focus', function(e){$(this).blur();});
});

其中#myModaltrigger是模式触发按钮的ID.使用.one()绑定的原因是,仅在显示模态之后才调用模糊函数.一旦它隐藏并且发生了焦点/模糊,就可以正常地对按钮进行聚焦,例如通过点按它,而不会使其自动模糊.

Where #myModaltrigger is the ID of the modal trigger button. The reason to use the .one() binding is so that the function to blur will be called only after the modal is shown. Once it hides, and the focus/blur happens, the button can be focused normally, like by tabbing to it, without it automatically blurring.

请参见> 此工作示例

这篇关于在引导程序中关闭模态后,如何取消模态触发按钮的焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆