简体   繁体   English

为什么jQuery .css('background-color')为'transparent'返回rgba(0,0,0,0)?

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

I have a box here - http://jsfiddle.net/U68p3/2/ - with a transparent background. 我这里有一个盒子 - http://jsfiddle.net/U68p3/2/ - 透明背景。 When I read the background with jQuery's .css('background-color') it returns 当我用jQuery的.css('background-color')读取背景时,它返回

               rgba(0, 0, 0, 0) 

which is not very helpful if my code is looking for a match with 'transparent'. 如果我的代码正在寻找与'transparent'匹配的话,这对我们没什么帮助。

Why is jQuery doing this and is there a way I can make it return 'transparent'? 为什么jQuery这样做,有没有办法让它返回'透明'?

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. 它不是jquery,颜色的计算值用RGBa(红色,蓝色,绿色,Alpha - 表示不透明度)表示,而不是作为颜色名称(如红色,蓝色,橙色,透明等)或十六进制值表示。 According to the specs transparency is represented as rgb(0, 0, 0) . 根据规格,透明度表示为rgb(0, 0, 0)

if the value is translucent, the computed value will be the rgba() corresponding one. 如果值是半透明的,则计算出的值将是rgba()对应的值。 If it isn't, it will be the rgb() corresponding one. 如果不是,它将是rgb()对应的一个。 The transparent keyword maps to rgb(0,0,0). transparent关键字映射到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. 因此,而不是寻找这个特定的值,你可以添加特定的CSS规则只包括透明度,类添加到元素,并使用.hasClass.is那个类的检查,如果该元素是透明的。

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. 似乎不同的浏览器以不同的方式表示它,IE,FF将值赋予transparency因此无论如何最好不依赖于任何逻辑的值表示。

It's not jQuery. 这不是jQuery。 jQuery returns what the browser gives it for the computed value (eg, from getComputedStyle or currentStyle ). jQuery返回浏览器为计算值提供的内容(例如,来自getComputedStylecurrentStyle )。 What the browser gives it may be in any notation form the browser wants to use. 浏览器提供的内容可以是浏览器想要使用的任何表示形式。 In this case, the browser on which you're testing uses rgba (red, green, blue, "alpha" — eg, opacity, 0 = transparent), which is a pretty good notation. 在这种情况下,您正在测试的浏览器使用rgba (红色,绿色,蓝色,“alpha” - 例如,不透明度,0 =透明),这是一个非常好的表示法。 Other browsers may just use rgb or even hex color strings, which would be unfortunate as neither of those can represent transparent correctly. 其他浏览器可能只使用rgb甚至十六进制颜色字符串,这将是不幸的,因为它们都不能正确表示transparent

You will not be able to get the color as a name directly from jQuery. 您将无法直接从jQuery获取颜色作为名称。

You can instead use the plugin jQuery Color ( https://github.com/jquery/jquery-color ) to convert the value to its name, or manually do something similar. 您可以使用插件jQuery Color( https://github.com/jquery/jquery-color )将值转换为其名称,或手动执行类似操作。

An example using your jsFiddle 使用jsFiddle的示例

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

( http://jsfiddle.net/Ap6qD/1/ ) http://jsfiddle.net/Ap6qD/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM