how to change background color of selected items i

2019-02-17 08:51发布

I want to give the yellow color to the selected items in multiselect dropdown box. By default it has gray background after selecting, I want to do this in HTML, CSS.

Can any one help in this?

6条回答
Explosion°爆炸
2楼-- · 2019-02-17 09:17

The following should work (for browsers that allow styling option tags), however the default select styling will override in most cases. You may be able to find a way to disable this however:

css

select option.selected {
  font-weight: bold;
  color: red;
}

javascript

function highlight_options(field){
  var i,c;
  for(i in field.options){
    (c=field.options[i]).className=c.selected?'selected':'';
  }
}

markup

<select onchange="highlight_options(this)" multiple="multiple">
  <option>One</option>
  <option>Two</option>
  <option>Three</option>
</select>
查看更多
叼着烟拽天下
3楼-- · 2019-02-17 09:24

Pure CSS would help here:

option:checked
查看更多
Summer. ? 凉城
4楼-- · 2019-02-17 09:34

You can't. The <option> element does not accept CSS styling.

You can used a JavaScript-based alternative. There are many <select> replacement scripts available.

查看更多
Bombasti
5楼-- · 2019-02-17 09:42

We can simply do with the help of the below css.

select option:checked{ background: #1aab8e -webkit-linear-gradient(bottom, #1aab8e 0%, #1aab8e 100%); }

查看更多
叛逆
6楼-- · 2019-02-17 09:42

We can use JS to select the DOMs.

$('select').change(function() {
    $('option').css('background', 'none');
    $('option:selected').css('backgroundColor', 'red');
}).change()

<select>
    <option>1111111</option>
    <option>222222222</option>
    <option>33333333</option>
    <option>44444444</option>
</select>

Demo : http://jsfiddle.net/TxbVt/1/

查看更多
叛逆
7楼-- · 2019-02-17 09:43
<style>
     .select2-container--default .select2-results__option[aria-selected=true] {
        background-color: inherit;
        color: lightgray;
    }
</style>

Add your own style inside the block.

查看更多
登录 后发表回答