how to change background color of selected items in multiselect dropdown box?

久未见 提交于 2019-12-01 01:27:59

问题


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?


回答1:


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

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




回答2:


<style>
     .select2-container--default .select2-results__option[aria-selected=true] {
        background-color: inherit;
        color: lightgray;
    }
</style>

Add your own style inside the block.




回答3:


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/




回答4:


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:

  • http://jsfiddle.net/DEDX7/
  • http://jsfiddle.net/DEDX7/1/

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>



回答5:


Pure CSS would help here:

option:checked



回答6:


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.



来源:https://stackoverflow.com/questions/13515767/how-to-change-background-color-of-selected-items-in-multiselect-dropdown-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!