How to add a specific class to select2 drop element?

后端 未结 4 2320
再見小時候
再見小時候 2021-02-18 18:23

I have select2 customized via css with its general classes and ids.

Now, I\'m trying to customize a specific class that will be provided to select2 and then apply in css

相关标签:
4条回答
  • 2021-02-18 18:36

    It's possible to make it in another way - via data-* of select2 - (if don't want to add select2.full.js). Use like it:

     var select2 = $(".element").select2({/* Select2 Opts */});
    

    Ok. Now push var select2 to console ( for understanding) like:

     console.log($(select2).data('select2'));
    

    You will see something like this:

    e {$element: init(1), id: "select2-....", options: e, listeners: Object, dataAdapter: d…}
     $container: init(1)
     $dropdown: init(1) <-- what you need!
     $element: init(1)
     $results: init(1)
     ....
    

    Ok, now just add specified class(-es) to select2's dropdown in the same way:

    select2.data('select2').$dropdown.addClass("...");
    

    Also, via $(select2).data('select2') you have access to others select2 elements (container, etc.).

    0 讨论(0)
  • 2021-02-18 18:45

    It will depend on the version of select2 that you are using. The html structure changed in one of the versions.

    Here is Fiddle example: https://jsfiddle.net/LpjcqbLu/

    I just put the 'error' class on the select box.

    .error + .select2-container, 
    .error + .select2-container + .select2-container .select2-dropdown {
        border: 3px solid red !important;
    }
    

    In earlier versions, the dropdown was not close to the select box (in the dom), so you would have to do a javascript solution to apply the error class directly.

    0 讨论(0)
  • 2021-02-18 18:47

    You can use dropdownCssClass for adding class to the select2-drop. I read whole plugin to understand what is going on. Finally, I found that option.

     $(".jSelectbox").select2({
         containerCssClass: "error",
         dropdownCssClass: "test"
     });
    

    $(".jSelectbox").select2({
      containerCssClass: "error",
      dropdownCssClass: "test"
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.js"></script>
    <select class="jSelectbox">
    			<option value="AL">Alabama</option>
    			<option value="AK">Alaska</option>
    			<option value="AZ">Arizona</option>
    			<option value="AR">Arkansas</option>
    			<option value="CA">California</option>
    			<option value="CO">Colorado</option>
    			<option value="CT">Connecticut</option>
    			<option value="DE">Delaware</option>
    			<option value="FL">Florida</option>
    			<option value="GA">Georgia</option>
    			<option value="HI">Hawaii</option>                   
    			<option value="ID">Idaho</option>
    			<option value="IL">Illinois</option>
    			<option value="IN">Indiana</option>
    			<option value="IA">Iowa</option>
    			<option value="KS">Kansas</option>
    			<option value="KY">Kentucky</option>
    			<option value="LA">Louisiana</option>
    			<option value="ME">Maine</option>
    			<option value="MD">Maryland</option>
    			<option value="MA">Massachusetts</option>
    			<option value="MI">Michigan</option>
    			<option value="MN">Minnesota</option>
    			<option value="MS">Mississippi</option>
    			<option value="MO">Missouri</option>
    			<option value="MT">Montana</option>
    			<option value="NE">Nebraska</option>
    			<option value="NV">Nevada</option>
    			<option value="NH">New Hampshire</option>
    			<option value="NJ">New Jersey</option>
    			<option value="NM">New Mexico</option>
    			<option value="NY">New York</option>
    			<option value="NC" selected="">North Carolina</option>
    			<option value="ND">North Dakota</option>
    			<option value="OH">Ohio</option>
    			<option value="OK">Oklahoma</option>
    			<option value="OR">Oregon</option>
    			<option value="PA">Pennsylvania</option>
    			<option value="RI">Rhode Island</option>
    			<option value="SC">South Carolina</option>
    			<option value="SD">South Dakota</option>
    			<option value="TX">Texas</option>
    			<option value="TN">Tennessee</option>
    			<option value="UT">Utah</option>
    			<option value="VT">Vermont</option>
    			<option value="VA">Virginia</option>
    			<option value="WA">Washington</option>
    			<option value="WV">West Virginia</option>
    			<option value="WI">Wisconsin</option>
    			<option value="WY">Wyoming</option>
    			</select>

    Jsfiddle

    0 讨论(0)
  • 2021-02-18 18:48

    containerCssClass works if you include select2.full.js. Full version has this option

    0 讨论(0)
提交回复
热议问题