得到元素真实的背景颜色的函数[javascript]
2006-08-01
这个函数来自Rico,自己改了一下。
说明:
传入参数一个,为元素的id值或元素本身,返回为元素的真实背景色值(字符串)。 值得一提的是IE里面返回的是16进制的值,而Mozilla则是rgb值。
演示地址: http://longbill.cn/down/sample/getbg.htm
阅读:3314 评论: 2 💬
说明:
传入参数一个,为元素的id值或元素本身,返回为元素的真实背景色值(字符串)。 值得一提的是IE里面返回的是16进制的值,而Mozilla则是rgb值。
演示地址: http://longbill.cn/down/sample/getbg.htm
function getBg(element)
{//author: Longbill (www.longbill.cn)
if (typeof element == "string") element = document.getElementById(element);
if (!element) return;
cssProperty = "backgroundColor";
mozillaEquivalentCSS = "background-color";
if (element.currentStyle)
var actualColor = element.currentStyle[cssProperty];
else
{
var cs = document.defaultView.getComputedStyle(element, null);
var actualColor = cs.getPropertyValue(mozillaEquivalentCSS);
}
if (actualColor == "transparent" && element.parentNode)
return arguments.callee(element.parentNode);
if (actualColor == null)
return "#ffffff";
else
return actualColor;
}
{//author: Longbill (www.longbill.cn)
if (typeof element == "string") element = document.getElementById(element);
if (!element) return;
cssProperty = "backgroundColor";
mozillaEquivalentCSS = "background-color";
if (element.currentStyle)
var actualColor = element.currentStyle[cssProperty];
else
{
var cs = document.defaultView.getComputedStyle(element, null);
var actualColor = cs.getPropertyValue(mozillaEquivalentCSS);
}
if (actualColor == "transparent" && element.parentNode)
return arguments.callee(element.parentNode);
if (actualColor == null)
return "#ffffff";
else
return actualColor;
}
阅读:3314 评论: 2 💬
#1
hxy
2006年08月20号 11:36
回复
?
#2
小狼诺夫
2006年09月24号 12:38
回复
还是因为标准不统一,写了这么长。
添加新的评论