我在做什么这里是非常简单的:怪异的行为时
当有人从一个选择选择一个选项,我想那选择的背景色更改为选择选项颜色。
$("select").change(function(){
var newcolor=$(this).children("option:selected").css("background-color");
$(this).css("backgroundColor", newcolor);
});
容易吧?那么它在Firefox 17.0.1中完全不起作用(它可以在Chrome中运行)。问题是变量newcolor被填充:rgb(51, 153, 255)
。最好的部分是这种颜色无处可找我的代码,CSS或任何东西。
我试过改变background-color
到backgroundColor
,children
到find
,没有什么作用。
有趣的部分是,如果我这样做:
$("select").change(function(){
var newcolor=$(this).children("option:first").css("background-color");
$(this).css("backgroundColor", newcolor);
});
并选择第一个选项,而不是选择一个...它的作品!
我可以使用一些类可能使它工作,但我很好奇,为什么会发生这种情况,并有任何方法来解决它?
编辑:增加了一个jsFiddle。用chrome和firefox试试吧!
你可以设置一个jsfiddle让我们看看发生了什么? –
[我可以强制jQuery.css(“backgroundColor”)以十六进制格式返回?](http://stackoverflow.com/questions/6177454/can-i-force-jquery-cssbackgroundcolor-returns-on-hexadecimal -format) –
添加了jsFiddle – Naryl