Change the background color of dropdownlist JQuery

2019-08-14 03:00发布

Below code doesn't seem to change the background color of dropdownlist. Please advice.

 var TextHighlightCss = {
        'background': '#FFFFAA',
        'border': '1px solid #FFAD33'
    };

 $('#ddlCarriers option:selected').css('TextHighlightCss');

Thanks in advance

BB

3条回答
Explosion°爆炸
2楼-- · 2019-08-14 03:12

Please do it like this, if you can help it:

CSS:

.hightlight {
  background: #FFFFAA;
  border: 1px solid #FFAD33;
}

JS:

$('#ddlCarriers option:selected').addClass("hightlight");

Guy who needs to refactor it in the future:

(n/a)

查看更多
地球回转人心会变
3楼-- · 2019-08-14 03:15
$('#ddlCarriers option:selected').css(TextHighlightCss);

You're passing a string, not your variable.

查看更多
够拽才男人
4楼-- · 2019-08-14 03:22

Remove the apostrophes from the .css() part and it should work.

Also, you may like to see this:

http://www.456bereastreet.com/lab/form_controls/select/

It shows the style differences between all browsers for select menus, quite handy.

查看更多
登录 后发表回答