为什么jQuery .css('background-color')返回rgba(0,0,0,0)为'transparent'? [英] Why does jQuery .css('background-color') return rgba(0,0,0,0) for 'transparent'?

查看:583
本文介绍了为什么jQuery .css('background-color')返回rgba(0,0,0,0)为'transparent'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个方块 - http://jsfiddle.net/U68p3/2/ - 有透明背景。当我用jQuery的.css('background-color')阅读背景时,它返回

  rgba(0,0, 0)

这不是很有帮助,如果我的代码正在寻找一个'transparent'匹配。 / p>

为什么jQuery会这样做,并且有一种方法可以让它返回'transparent'?



谢谢。

  $(function(){
var bkgnd = $('#box')。css color');
console.log('background-color is'+ bkgnd);
});


解决方案

它不是jquery,颜色的计算值以RGBa(红色,蓝色,绿色,Alpha - 用于不透明度)表示,而不是以颜色名称(如红色,蓝色,橙色,透明等)或十六进制值表示。根据规范透明度表示为 rgb( 0,0,0)


如果值是半透明的,计算的值将是rgba )对应一个。如果不是,就会是rgb()对应的一个。透明关键字映射到rgb(0,0,0)。


因此,不必查找此特定值, css规则只是为了包含透明度,并将该类添加到元素并使用该类的 .hasClass .is 检查元素是否透明。



看起来不同的浏览器以不同的方式表示它,IE,FF给出的值为 transparency 因此最好不要依赖这个值表示任何逻辑。


I have a box here - http://jsfiddle.net/U68p3/2/ - with a transparent background. When I read the background with jQuery's .css('background-color') it returns

               rgba(0, 0, 0, 0) 

which is not very helpful if my code is looking for a match with 'transparent'.

Why is jQuery doing this and is there a way I can make it return 'transparent'?

Thanks.

$(function() {
    var bkgnd = $('#box').css('background-color');
    console.log('background-color is ' + bkgnd);
});

解决方案

It is not jquery, the computed value for the color are represented in RGBa (Red, Blue, Green, Alpha - for opacity) and not in as color names (like red, blue, orange, transparent etc) or as hex values. According to the specs transparency is represented as rgb(0, 0, 0).

if the value is translucent, the computed value will be the rgba() corresponding one. If it isn't, it will be the rgb() corresponding one. The transparent keyword maps to rgb(0,0,0).

So instead of looking for this specific value you can add a specific css rule just to include transparency and add that class to the element and use .hasClass or .is of that class to check if the element is transparent.

It seems like different browsers represent it in different ways, IE, FF gives the value as transparency so it is anyways better not to rely on this value representation for any logic.

这篇关于为什么jQuery .css('background-color')返回rgba(0,0,0,0)为'transparent'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆