使用window.getComputedStyle(obj, null).backgroundColor取背景色没有背景色和黑色取得的结果都是rgba(0, 0, 0)
有什么好办法可以区分出来?测试代码<html>
<head>
<title>test get bgcolor</title>
<script>
function getBgColor(obj)
{
return window.getComputedStyle(obj, null).backgroundColor;
}
</script>
</head>
<body>
<div id='test1' style='width: 400px; height: 100px; cursor: pointer;' onclick="alert(getBgColor(this));"></div>
<div id='test1' style='width: 400px; height: 100px; cursor: pointer; background-color: #000' onclick="alert(getBgColor(this));"></div>
</body>
</html>
有什么好办法可以区分出来?测试代码<html>
<head>
<title>test get bgcolor</title>
<script>
function getBgColor(obj)
{
return window.getComputedStyle(obj, null).backgroundColor;
}
</script>
</head>
<body>
<div id='test1' style='width: 400px; height: 100px; cursor: pointer;' onclick="alert(getBgColor(this));"></div>
<div id='test1' style='width: 400px; height: 100px; cursor: pointer; background-color: #000' onclick="alert(getBgColor(this));"></div>
</body>
</html>
是符合标准的:http://www.w3.org/TR/CSS2/colors.html#background-properties
<head>
<title>test get bgcolor</title>
<script>
function getBgColor(obj)
{
return obj.style.backgroundColor ? obj.style.backgroundColor : "未设定";
}
</script>
</head>
<body>
<div id='test1' style='width: 400px; height: 100px; cursor: pointer;' onclick="alert(getBgColor(this));"></div>
<div id='test1' style='width: 400px; height: 100px; cursor: pointer; background-color: #000' onclick="alert(getBgColor(this));"></div>
</body>
</html>
在FF3.6.8下上点上部份弹出Transparent,下半部弹出rgb(0,0,0)
在Chrome,分别是rgba(0,0,0,0),rgb(0,0,0)不管怎么样返回都不一样吧.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<script>
function getBgColor(obj)
{ return document.defaultView.getComputedStyle(obj, null).backgroundColor;
}
</script><body> <div id='test1' style='width: 400px; height: 100px; cursor: pointer;' onclick="alert(getBgColor(this));"></div>
<div id='test1' style='width: 400px; height: 100px; cursor: pointer; background-color: #000' onclick="alert(getBgColor(this));"></div>
</body>
</html>没有背景色的是 transparent
我用的是chrome。
没注意多个0,感谢楼上的提示。