网络编程
位置:首页>> 网络编程>> JavaScript>> 得到元素真实的背景颜色的函数

得到元素真实的背景颜色的函数

作者:Longbill  发布时间:2008-05-20 12:04:00 

标签:函数,背景色,js

传入参数一个,为元素的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; 
}
0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com