How to change the css of color of select2 tags?

后端 未结 8 1856
萌比男神i
萌比男神i 2020-12-31 06:47

I just started using project for showing multiple tags from a select box and it works great, thanks for the library.

I just need to modify the color or css of the ta

8条回答
  •  有刺的猬
    2020-12-31 07:30

    First up - a warning that this means you are overriding the CSS that is internal to select2, so if select2 code changes at a later date, you will also have to change your code. There is no formatChoiceCSS method at the moment (though it would be useful).

    To change the default color, you will have to override the various CSS properties of the tag which has this CSS class:

    .select2-search-choice {
        background-color: #56a600;
        filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f4f4f4', endColorstr='#eeeeee', GradientType=0 );
        background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, color-stop(20%, #f4f4f4), color-stop(50%, #f0f0f0), color-stop(52%, #e8e8e8), color-stop(100%, #eeeeee));
        background-image: -webkit-linear-gradient(top, #f4f4f4 20%, #f0f0f0 50%, #e8e8e8 52%, #eeeeee 100%);
        background-image: -moz-linear-gradient(top, #f4f4f4 20%, #f0f0f0 50%, #e8e8e8 52%, #eeeeee 100%);
        background-image: -o-linear-gradient(top, #f4f4f4 20%, #f0f0f0 50%, #e8e8e8 52%, #eeeeee 100%);
        background-image: -ms-linear-gradient(top, #f4f4f4 20%, #f0f0f0 50%, #e8e8e8 52%, #eeeeee 100%);
        background-image: linear-gradient(top, #f4f4f4 20%, #f0f0f0 50%, #e8e8e8 52%, #eeeeee 100%);
        -webkit-box-shadow: 0 0 2px #ffffff inset, 0 1px 0 rgba(0,0,0,0.05);
        -moz-box-shadow: 0 0 2px #ffffff inset, 0 1px 0 rgba(0,0,0,0.05);
        box-shadow: 0 0 2px #ffffff inset, 0 1px 0 rgba(0,0,0,0.05);
        color: #333;
        border: 1px solid #aaaaaa;
    }
    

    To change the class of each tag based on the text or option # of that tag, you will have to add a change event:

    $("#select2_element").on("change", function(e) { 
          if (e.added) {
              // You can add other filters here like
              // if e.val == option_x_of_interest or
              // if e.added.text == some_text_of_interest
              // Then add a custom CSS class my-custom-css to the 
  • added $('.select2-search-choice:not(.my-custom-css)', this).addClass('my-custom-css'); } });
  • And you can define a custom background-color etc in this class:

    li.my-custom-css {
           background-color: // etc etc
    }
    

提交回复
热议问题