How to remove the arrow from a select element in Firefox

后端 未结 30 1722
北恋
北恋 2020-11-22 15:58

I\'m trying to style a select element using CSS3. I\'m getting the results I desire in WebKit (Chrome / Safari), but Firefox isn\'t playing nicely (I\'m not ev

30条回答
  •  醉酒成梦
    2020-11-22 16:33

    If you don't mind fiddling with JS, I wrote a small jQuery plugin that helps you do it. With it you don't need to worry about vendor prefixes.

     $.fn.magicSelectBox = function() {
      var $e = this;
    
      $e.each(function() {
        var $select = $(this);
    
        var $magicbox = $('
    ').attr('class', $select.attr('class')).attr('style', $select.attr('style')).addClass('magicbox'); var $innermagicbox = $('
    ').css({ position: 'relative', height: '100%' }); var $text = $('').css({ position: 'absolute' }).text($select.find("option:selected").text()); $select.attr('class', null).css({ width: '100%', height: '100%', opacity: 0, position: 'absolute' }).on('change', function() { $text.text($select.find("option:selected").text()); }); $select.parent().append($magicbox); $innermagicbox.append($text, $select); $magicbox.append($innermagicbox); }); return $e; };

    Fiddle here: JS Fiddle

    The condition is that you have to style the select from scratch (this means setting the background and border), but you probably want to do this anyway.

    Also since the function substitutes the original select with a div, you will lose any styling done directly on the select selector in your CSS. So give the select element a class and style the class.

    Supports most modern browsers, if you want to target older browsers, you can try an older version of jQuery, but perhaps have to replace on() with bind() in the function (not tested)

提交回复
热议问题