仅更改所选选项的颜色

我有一个坐在桌子上的选择器。 表行有颜色,所以使用CSS我可以使用background-color:inherit;将下拉的背景更改为相同background-color:inherit; 但是,它会使用所有选项更改整个框的颜色。

是否可以仅更改所选选项的颜色,如果没有CSS可能借助jQuery?

使用jQuery可以实现一切:)你应该试试这个:

 $('.mySelect').change(function () { $(this).find('option').css('background-color', 'transparent'); $(this).find('option:selected').css('background-color', 'red'); }).trigger('change'); 

这是一个现场演示

希望有所帮助!

我能够通过首先将SELECT元素的背景颜色设置为我想要的结果来实现,从而导致所有选项都是该颜色。 然后我将所有选项都设为特定的配色方案。 最后,我使所选选项与SELECT元素具有相同的颜色方案,因此该选项在下拉列表中显示相同的颜色。

 $.each($('select'), function(i,v) { theElement = $(v); theID = theElement.attr('id'); // Set the SELECT input element to green background with white text theElement.css('background-color', 'green'); theElement.css('color', 'white'); // Set all the options to another color (note transparant will show the green) $('#'+theID).find('option').css('background-color', 'white'); $('#'+theID).find('option').css('color', 'black'); // Finally set the selected option to the same values as the SELECT element $('#'+theID).find('option:selected').css('background-color', 'green'); $('#'+theID).find('option:selected').css('color', 'white'); }); 

你应该能够用jQuery做到这一点。 我可能错了(参见David Thomas的评论),但试试:

 $("option[value='myValue']").css('background-color', 'red');