admin管理员组

文章数量:1289527

Is it possible to change "selector" color in drop-down list?

 <select name="select" style="background-color: #ff0000">
 <option style="background-color: #ff0000" value="1">Red</option>
 <option style="background-color: #ffffff" value="2">Green</option>
 <option style="background-color: #0000ff" value="3">Blue</option>
 </select>

I tried in above style but it didn't worked. I know with JavaScript getting document.getElementById('text').style.color='red' can set the color.

But is it possible in HTML to set the colors?

Is it possible to change "selector" color in drop-down list?

 <select name="select" style="background-color: #ff0000">
 <option style="background-color: #ff0000" value="1">Red</option>
 <option style="background-color: #ffffff" value="2">Green</option>
 <option style="background-color: #0000ff" value="3">Blue</option>
 </select>

I tried in above style but it didn't worked. I know with JavaScript getting document.getElementById('text').style.color='red' can set the color.

But is it possible in HTML to set the colors?

Share Improve this question edited Feb 8, 2011 at 8:42 Brock Adams 93.6k23 gold badges241 silver badges305 bronze badges asked Feb 8, 2011 at 7:20 JudyJudy 1,5539 gold badges27 silver badges41 bronze badges 1
  • 1 AFAIK there isn't a cross patible way of acplishing this without the aid of Javascript. i.e. Styling each option value. – Russell Dias Commented Feb 8, 2011 at 8:14
Add a ment  | 

3 Answers 3

Reset to default 6

To preserve the background color of the selected item (which I believe is what you're after) add the following onchange code:

<select name="select" style="background-color: #ff0000" onchange="this.style.backgroundColor = this.options[this.selectedIndex].style.backgroundColor;">

Live test case: http://jsfiddle/yahavbr/LD8Dx/

its work correctly in many browsers. I didnt see any problem with this HTML.

It works for me in Google Chrome and Mozilla Firefox.

Edit: also works in IE6

本文标签: javascriptChanging color of a row in dropdown listStack Overflow