【问题标题】:firefox css bug/issuefirefox css错误/问题
【发布时间】:2012-09-08 09:15:34
【问题描述】:

在 Chrome 和 IE7-9 运行良好的同时,firefox 带来了另一个惊喜......

http://jsfiddle.net/D3zXj/

我要做的是根据所选option 的颜色更改Select 的颜色。由于某种神秘的原因,它在 Firefox 中不起作用。

有什么想法吗?

$('#selectstat_sch').change(function(){
    $(this).css('color',$('option:selected',this).css('color'));
}).trigger('change');

【问题讨论】:

    标签: jquery html css firefox select


    【解决方案1】:

    详细说明未定义,我们可能希望首先验证我们的 css 属性,

    如果存在其他一些属性,这也应该有效。

    http://jsfiddle.net/D3zXj/2/

        var style=$('option:selected',this).attr('style');
        var matches=style.match(/color:([^;]+)/);
        if (matches) $(this).css('color',matches[1]);
        //else alert('no match');
    

    【讨论】:

    • 是的,这是更好的技术。
    【解决方案2】:

    使用 attr('style') 代替 css('color'):

    $(document).ready(function(){
        $('#selectstat_sch').change(function(){
            $(this).attr('style',$('option:selected').attr('style'));
        });
    });
    

    但是这个副本会覆盖整个样式。

    【讨论】:

    • 呵呵,谢谢,这个简单又不失简洁。可能也适合这个目的。
    【解决方案3】:

    选择该选项时,它会比HTML中的一个不同的计算颜色,因为有一个样式所选选项的UA表规则。实际上,您可以看到:所选选项是蓝色背景的白色。

    因此,您可能只想获得.style.color,而不是从选项中获取计算的颜色,因为这正是您想要的,

    【讨论】:

      猜你喜欢
      • 2021-07-12
      • 1970-01-01
      • 2012-09-10
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 2013-03-07
      • 2010-11-09
      • 2012-07-21
      相关资源
      最近更新 更多