简体   繁体   English

Bootstrap网格更改布局时,如何更改选择元素的大小?

[英]How do I change the size of a select element when Bootstrap grid changes layout?

I have built a webpage using Bootstrap. 我已经使用Bootstrap建立了一个网页。 Currently, I have a few <select> elements with size=6 in a col-md-3 on the left and some other stuff in a col-md-9 on the right. 目前,我在左侧的col-md-3有几个<select>元素,它们的size=6 ,在右侧的col-md-9中有一些其他的东西。

If one shrinks the size of the window, eventually the right column collapses underneath the left column (becoming rows instead of columns). 如果缩小窗口的大小,则右列最终会在左列下方折叠(成为行而不是列)。

When this happens, I would like to decrease the <select> size (changing it to size=3 ). 发生这种情况时,我想减小<select>大小(将其更改为size=3 )。 I'd like the reverse to happen as well (when the window is re-expanded into columns, the <select> elements return to size=6 ). 我也希望发生相反的情况(当窗口重新扩展为列时, <select>元素将返回size=6 )。

I would like to do a similar thing for another element (height instead of size), but I figure that if I can figure out size, I can figure out height. 我想对另一个元素(高度而不是尺寸)做类似的事情,但是我认为,如果我能确定尺寸,就可以确定高度。

How do I change an attribute when bootstrap shifts like that? 引导程序移动时,如何更改属性?

This is a simplified snippet from the code. 这是代码的简化片段。 I am using Bootstrap 3. 我正在使用Bootstrap 3。

<div class="container-fluid">
            <div class="row">
                <div class="col-md-3">
                    <div id="flight-selection-div" class="panel-group">
                        <div class="panel panel-primary flight-panel">
                            <div class="panel-heading flight-panel">
                                <h4 class="panel-title">Flights</h4>
                            </div>
                            <div class="panel-body">
                                <select size=6 class="form-control" id="flight-select" name="flight">
                                    <option value=null disabled>Select a flight here</option>
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="panel-group">
                        <div class="panel panel-primary map-panel">
                            <div class="panel-heading map-panel">
                                <h4 class="panel-title">Map</h4>
                            </div>
                            <div class="panel-body">
                                <div id="map"></div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="col-md-9">
                    <img src="fisher2.png" class="img-responsive" id="img-ele" alt="Your data will appear here">
                </div>
            </div>
        </div>

Do you need specifically to use the size attribute? 您是否需要专门使用size属性? Why don't you style your select width based on CSS width and media queries? 为什么不基于CSS宽度和媒体查询来设置选择宽度的样式?

You can use the same media queries that bootstrap framework uses and set a width to your select according to the screen size you want: 您可以使用与引导框架相同的媒体查询,并根据所需的屏幕大小为您的选择设置宽度:

See this answer: https://stackoverflow.com/a/30542215/8419161 看到这个答案: https : //stackoverflow.com/a/30542215/8419161

I would recommend using jQuery for this (which you must be using already if you are using Bootstrap). 我建议为此使用jQuery(如果使用Bootstrap,则必须已经使用jQuery)。 I got this piece of code from here and modified it some to give you a sense of what you can do: 我从这里获得了这段代码,并对其进行了一些修改,以使您对可以做什么有所了解:

$(window).resize(function() {
    if (window.innerWidth < 480) {

        $('#flight-select').replaceWith('<select size=3 class="form-control" id="flight-select" name="flight">');

     } else{

       $('#flight-select').replaceWith('<select size=6 class="form-control" id="flight-select" name="flight">');    

     }
        }).resize();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM