javascript 关闭后模糊 Select2 输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21157181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:14:35  来源:igfitidea点击:

Blur Select2 input after close

javascriptjqueryjquery-select2

提问by Akos K

I want the select2 element to lose the focus when the select2-close event is triggered, what I have tried so far was:

我希望 select2 元素在 select2-close 事件被触发时失去焦点,到目前为止我尝试过的是:

.on("select2-close", function (e) {
    var $focused = $(':focus');
    $focused.blur();
});

and some variations to get focused element like document.activeElement, $(e.target) non f these worked.

和一些变体以获得焦点元素,如document.activeElement$(e.target) non f 这些工作。

JSFiddle

JSFiddle

回答by James Donnelly

You need to remove the .select2-container-activeclass from the containing divider:

您需要.select2-container-active从包含分隔符中删除该类:

.on("select2-close", function () {
    setTimeout(function() {
        $('.select2-container-active').removeClass('select2-container-active');
        $(':focus').blur();
    }, 1);
});

I've used a setTimeouthere as a hacky way to ensure that this triggers after the plugin itself finalises the close.

我使用了一个setTimeouthere 作为一种hacky 方式来确保在插件本身完成关闭之后触发。

JSFiddle demo.

JSFiddle 演示