How to change the text color of first select option

后端 未结 6 903
我在风中等你
我在风中等你 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:41

    I really wanted this (placeholders should look the same for text boxes as select boxes!) and straight CSS wasn't working in Chrome. Here is what I did:

    First make sure your select tag has a .has-prompt class.

    Then initialize this class somewhere in document.ready.

    # Adds a class to select boxes that have prompt currently selected.
    # Allows for placeholder-like styling.
    # Looks for has-prompt class on select tag.
    Mess.Views.SelectPromptStyler = Backbone.View.extend
      el: 'body'
    
      initialize: ->
        @$('select.has-prompt').trigger('change')
    
      events:
        'change select.has-prompt': 'changed'
    
      changed: (e) ->
        select = @$(e.currentTarget)
        if select.find('option').first().is(':selected')
          select.addClass('prompt-selected')
        else
          select.removeClass('prompt-selected')
    

    Then in CSS:

    select.prompt-selected {
      color: $placeholder-color;
    }
    

提交回复
热议问题