我这里有一个盒子 –
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将值赋予透明度,因此无论如何最好不依赖于任何逻辑的值表示.