How to change the text color of first select option

后端 未结 6 908
我在风中等你
我在风中等你 2020-11-30 10:49

I have a select element which has several items. I want to change the color of its first item, but it seems the color only shows when you click on the select dropdown. What

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 11:26

    Here is a way so that when you select an option, it turns black. When you change it back to the placeholder, it turns back into the placeholder color (in this case red).

    http://jsfiddle.net/wFP44/166/

    It requires the options to have values.

    $('select').on('change', function() {
      if ($(this).val()) {
    return $(this).css('color', 'black');
      } else {
    return $(this).css('color', 'red');
      }
    });
    select{
      color: red;
    }
    select option { color: black; }
    
    

提交回复
热议问题