javascript 如何将ListBox的高度设置为自动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4050368/
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
How to set height of ListBox to auto
提问by Niraj Choubey
I have a Html ListBox:
我有一个 Html 列表框:
<select id="targetField" multiple="multiple" name="D1" style="width:200px;">
<option>INDIA</option>
<option>USA</option>
<option>UK</option>
<option>AUSTRALIA</option>
<option>RUSSIA</option>
<option>FRANCE</option>
<option>HOLLAND</option>
</select>
I need to set the height of this to auto i.e I do not want scrollbar to appear.
我需要将此高度设置为自动,即我不希望出现滚动条。
I tried Height:auto;But it is not working in IE.
我试过Height:auto;但它在 IE 中不起作用。
How should I do this?
我该怎么做?
回答by Nick Craver
Set the sizepropertyto the number of items, like this:
将size属性设置为项目数,如下所示:
<select id="targetField" multiple name="D1" style="width:200px;" size="7">
If you need to do it programmatically, you can set all <select>elements to their option length, like this:
如果您需要以编程方式执行此操作,您可以将所有<select>元素设置为其选项长度,如下所示:
$("select").attr("size", function() { return this.options.length; });
回答by jerjer
Set the size attribute equal to the number of items(options).
将 size 属性设置为项目数(选项)。
var select = $('#targetField');
select.attr('size', select[0].options.length);
回答by Jeff Norman
You can modify it using 'line-height' eighter and putting values to 'height'.
您可以使用“line-height”八进制修改它并将值设置为“height”。
<select id="targetField" multiple="multiple" name="D1" style="width:200px; line-height:27px; float:left; height:130px;">
<option>INDIA</option>
<option>USA</option>
<option>UK</option>
<option>AUSTRALIA</option>
<option>RUSSIA</option>
<option>FRANCE</option>
<option>HOLLAND</option>
</select>
Hope this helps.
希望这可以帮助。
回答by Chinmayee G
try with height:100%. It is working in ie6
尝试使用height:100%. 它在 ie6 中工作

