1

我使用创建了以下下拉列表

<select></select>

标签。我使用的代码是:

<html>
<body>
<select name="ans" >
<option> select 1</option>
<option> select 2</option>
</select>
</body>
</html>

他们是我可以更改列表样式的一种方式,例如下拉箭头或其中的文本。

4

1 回答 1

3

如果您想设置下拉按钮的样式,您可以提出以下方法。这个想法是通过将其不透明度降低到 0 来隐藏原始选择,但仍保留其功能。为此,我们还需要一点 JS(只需一点点),以便在您更改 select 中的 options-value 时更改 Text-value。

CSS:

.selectWrap {
  /* Style your own select-box here */
  background: #ddd;
  border: 1px solid black;
  color: #333;

  /* Your new Arrow */
  /* Created with the after-pseudo-element to save Markup,
     Styled the arrow with help of the border-trick to provide Retina-ready arrow */
  &:after {
    border-width: 6px;
    border-style: solid;
    border-color: transparent transparent transparent #000;
    content: "";
    right: 20px;
    position: absolute;
    top: 20px;
  }

  height: 30px;
  position:relative;
}


/* Hide the original select */
select {
  height: 30px;
  left: 0;
  position: absolute;
  top: 0;
  width: 100%;

  /* Hide the select cross-browser */
  -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
  filter: alpha(opacity=0);
  -moz-opacity: 0.0;
  -khtml-opacity: 0.0;
  opacity: 0.0;
}

的HTML:

<div class="selectWrap">
  <select>
    <option>one</option>
    <option>two</option>
  </select>

  <div class="selectText">Please choose..</div>
</div>

JavaScript(jQuery):

/* Always when the option in the select is changed, change the text of our selectWrap */
$(document).ready(function () {
  $('.selectWrap select').on('change', function (e) {
     var wrap = $(e.target).parents('.selectWrap');
     wrap.find('.selectedText').html(this.options[this.selectedIndex].innerHTML);
  });
});
于 2013-02-12T08:59:58.787 回答