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

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

问题描述

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

               rgba(0, 0, 0, 0) 

如果我的代码正在寻找与透明"匹配的内容,这不是很有帮助.

为什么jQuery会这样做,并且有办法让它返回透明"?

谢谢.

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

解决方案

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

如果该值是半透明的,则计算出的值将是对应的rgba().如果不是,它将是对应的rgb().透明关键字映射到rgb(0,0,0).

因此,除了查找该特定值之外,您还可以添加特定的css规则以仅包括透明性并将该类添加到元素中,并使用该类的.hasClass.is来检查该元素是否透明./p>

似乎不同的浏览器以不同的方式表示它,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')为' transparent'返回rgba(0,0,0,0)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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