是jquery自动将rgba转换为rgb?

Cle*_*ent 5 javascript rgb jquery rgba

我是jquery的新手,当我写作时,我有点惊讶

$('#selected-color').css('background-color', 'rgba(0,0,0,1)');
Run Code Online (Sandbox Code Playgroud)

我的最终风格最终#selected-color结束了rgb(0,0,0).当我得到背景颜色并尝试在其上应用正则表达式时,这会产生很大的不同,期望得到一个rgba字符串.

所以问题是当alpha设置为1时,Jquery是否对rgba值应用自动转换?我得到了相同的结果a = 1.0但我得到了一个= 0.99的RGBA.我在jquery文档中没有成功的迹象

Ang*_*tis 0

这不是一个jQuery功能。所有颜色表达式都被计算为rgb表达式whenalpha = 1rgba表达式when alpha < 1

\n\n

来自MDN 文档

\n\n
\n

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

\n
\n\n

在CSS3 规范中:

\n\n
\n

计算值:

\n\n
    \n
  • 基本颜色关键字、RGB 十六进制值和扩展颜色关键字的计算值是 RGB 数值值的等效三元组,例如六位十六进制值或 rgb(...) 函数值,alpha 值为 1。\n
  • \n
  • 关键字\xe2\x80\x98transparent\xe2\x80\x99 的计算值是所有零数值RGBA 值的四元组,例如rgba(0,0,0,0)。
  • \n
  • 对于所有其他值,计算值是指定值。
  • \n
\n
\n\n

例子:

\n\n

\r\n
\r\n
var\r\n  foo = document.getElementById("foo"),\r\n  getBGColor = function () {\r\n    return getComputedStyle(foo, null).backgroundColor;\r\n  };\r\n\r\n/* Setting the background with an alpha value of 1. */\r\nfoo.style.backgroundColor = "rgba(0, 0, 0, 1)";\r\nconsole.log("alpha = 1: ", getBGColor());\r\n\r\n/* Setting the background with a hex number. */\r\nfoo.style.backgroundColor = "#000";\r\nconsole.log("alpha = 1: ", getBGColor());\r\n\r\n/* Setting the background with a hsl expression. */\r\nfoo.style.backgroundColor = "hsl(120, 100%, 25%)";\r\nconsole.log("alpha = 1: ", getBGColor());\r\n\r\n/* Setting the background with an alpha value less than 1. */\r\nfoo.style.backgroundColor = "rgba(0, 0, 0, .5)";\r\nconsole.log("alpha < 1: ", getBGColor());\r\n\r\n/* Setting the background with a hsla expression. */\r\nfoo.style.backgroundColor = "hsla(120, 100%, 25%, .5)";\r\nconsole.log("alpha < 1: ", getBGColor());
Run Code Online (Sandbox Code Playgroud)\r\n
<div id = "foo"></div>
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n