使用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>

解决方案 »

  1.   

    不会啊,没有背景色是 transparent 
    是符合标准的:http://www.w3.org/TR/CSS2/colors.html#background-properties
      

  2.   

    <html>
        <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>
      

  3.   

    这个代码在IE6下报错,
    在FF3.6.8下上点上部份弹出Transparent,下半部弹出rgb(0,0,0)
    在Chrome,分别是rgba(0,0,0,0),rgb(0,0,0)不管怎么样返回都不一样吧.
      

  4.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <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 
      

  5.   

    我还以为Firefox会和chrome一样呢。呵呵
    我用的是chrome。
    没注意多个0,感谢楼上的提示。