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

我这里有一个盒子 – http://jsfiddle.net/U68p3/2/ – 背景透明.当我用jQuery的.css(‘background-color’)读取背景时,它返回
rgba(0,0)

如果我的代码正在寻找与’transparent’的匹配,那么这不是很有帮助.

为什么jQuery这样做,有没有办法让它返回’透明’?

谢谢.

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

解决方法

它不是jquery,颜色的计算值用RGBa(红色,蓝色,绿色,Alpha – 表示不透明度)表示,而不是以颜色名称(如红色,橙色,透明等)或十六进制值表示.根据 specs透明度表示为rgb(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).

因此,您可以添加特定的css规则以包含透明度并将该类添加到元素中,并使用该类的.hasClass或.is来检查元素是否透明,而不是查找此特定值.

似乎不同的浏览器以不同的方式表示它,IE,FF将值赋予透明度,因此无论如何最好不依赖于任何逻辑的值表示.

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: <span id=&quot...
jQuery 添加水印 <script src="../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...