11

如何限制选择元素的下拉列表的高度 - 这样如果选项的总数大于此高度 - 我应该在下拉列表中滚动。如果我能在像素或项目数量方面做到这一点,我会很满意。

所以说我有以下html标记:

<select>
    <option selected>Select</option>
    <option>This is an option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
</select>

我将如何只显示前 4 个选项和滚动中的其余部分。

是我到目前为止所拥有的,但它不起作用。

4

2 回答 2

11

在 StackOverflow 上搜索,我遇到了这个. 可悲的是,如果您想将其保留为下拉框,则无法在 CSS 中实现您想要的。如链接中所述,JavaScript 或 jQuery 可以解决问题,或者您可以在标签size上使用该属性select,但这会破坏下拉框的外观。

于 2012-11-13T14:32:54.267 回答
-3

我在 CSS+HTML 中找到了答案,你可以这样做:

<select>
  <optgroup style="max-height: 65px;" label="">
    <option selected>Select</option>
    <option>This is an option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
    <option>This is another Option</option>
  </optgroup>
</select>
于 2013-04-25T12:01:00.120 回答