jQuery 如何使用多行选项构建 <select>?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15368502/
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-08-26 14:55:03  来源:igfitidea点击:

How can I build a <select> with multiline options?

javascriptjqueryhtmlcss

提问by harrison4

I need to build a multi-line select control. All the <option>'s where text is wider than 300px (for example) become to have the necessary lines to don't exceed the mentioned limit.

我需要构建一个多行选择控件。<option>文本宽于 300 像素(例如)的所有's 都具有不超过上述限制的必要行。

These images speak for themselves:

这些图像不言自明:

http://home.comcast.net/~ephmynus/select1.jpghttp://home.comcast.net/~ephmynus/select2.jpg

http://home.comcast.net/~ephmynus/select1.jpghttp://home.comcast.net/~ephmynus/select2.jpg



First of all I tried this, but does not work.

首先我试过这个,但不起作用。

<html>
    <head>
        <style>
            option{
                width: 100px; 
                white-space: wrap;
            }

        </style>
    </head>
    <body>
        <select>
            <option>I'm short</option>
            <option>I'm medium text</option>
            <option>I'm a very very very very very very very very very long text</option>
        </select>   
    </body>
</html>

I'm using bootstrap framework and I thought that maybe I could use dropdown control to obtain the result. I've tried setting the max-width CSS property, but it does not work too...

我正在使用引导程序框架,我想也许我可以使用下拉控件来获得结果。我试过设置 max-width CSS 属性,但它也不起作用......

How can I achieve this?

我怎样才能做到这一点?

回答by Diodeus - James MacFarlane

<option>tags have limited styling capabilities and do not support formatting.

<option>标签的样式功能有限,不支持格式化。

You can use a JavaScript-based alternative. There are plenty out there if you look around.

您可以使用基于JavaScript 的替代方法。如果你环顾四周,那里有很多。