html - Select2 宽度问题,控件覆盖父宽度

标签 html css twitter-bootstrap ui-select2

我在 Bootstrap 网格中使用 Select2 控件,但是当我从下拉列表中选择一个较大的值(这里我指的是宽度)时,它会扩展并覆盖由“col-md”指定的宽度" Bootstrap 类

though there are many workarounds mentioned on a similar issue https://github.com/t0m/select2-bootstrap-css/issues/42

但他们都不适合我

请指导。我如何将宽度限制为其容器元素的宽度

最佳答案

我还尝试了各种建议的解决方法,但没有效果。此外,我需要 select2 控件在调整窗口大小时正确调整大小,因此我定义了一个 reset_select2_size 函数,每次调整大小时都会调用该函数。尽管此处未显示,但每当 UI 组件中的任何更改需要重置该组件内的 select2 大小时(例如,当包含 select2 的隐藏 div 变得可见时),也应该调用此函数。

从任何标准来看,这都不是很好,但在修复错误之前,这对我来说似乎工作正常,

function reset_select2_size(obj)
{
    if (typeof(obj)!='undefined') {
        obj.find('.select2-container').parent().each(function() {
            $(this).find('.select2-container').css({"width":"10px"});
        });

        obj.find('.select2-container').parent().each(function() {
            var width = ($(this).width()-5)+"px";
            $(this).find('.select2-container').css({"width":width});
        });
        return;
    }

    $('.select2-container').filter(':visible').parent().each(function() {
        $(this).find('.select2-container').css({"width":"10px"});
    });
    $('.select2-container').filter(':visible').parent().each(function() {
        var width = ($(this).width()-5)+"px";
        $(this).find('.select2-container').css({"width":width});
    });
}

function onWindowResized( event )
{
    reset_select2_size();
}

window.addEventListener('resize', onWindowResized );

关于html - Select2 宽度问题,控件覆盖父宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31783403/

相关文章:

html - Bootstrap 表格图像作为边框

html - 在 ul 内的图像顶部覆盖列表项元素,而无需设置背景图像

css - Twitter Bootstrap btn-primary 尺寸 IE8

javascript - 私有(private)菜单,大小灵活

javascript - 没有新文件的 react 组件的内联 css

javascript - 具有多个元素的 Bootstrap 5 轮播

html - DIV 背景中的错误未覆盖垂直菜单

javascript - Bootstrap 下拉菜单不起作用

html - 防止格式错误的内容

html - 以编程方式从 html5 中的上传中删除 1 个或多个文件?