|
|
Retrieving background-color Property of CSS Elements - CSS
|
Views : 1035
|
|
Tagged in : CSS
|
|
|
Report This Scrap as Inappropriate We request you to choose the appropriate categroy and subcategory that suits your
objectionable concern about the scrap, So that our team can review and find out whether it violates our Guidelines or the
scrap is not suitable for all viewers.
|
This describes a method for retrieving the background-color values of a html element
In Mozilla browsers (like Firefox), the color can be retrieved using
getComputedStyle(element, psedoStyleString).
For example, to get the background-color for this <div>,
A <div> with yellow background rgb(255,255,0) or #ffff00.
you'd use:
var element=document.getElementById("testDiv");
var style=window.getComputedStyle(element,"");
var bgColor=style.getPropertyValue("background-color");
Now, combining the two methods:
function getBgColor(element)
{
if (element.currentStyle)
return element.currentStyle.backgroundColor;
if (window.getComputedStyle)
{
var elementStyle=window.getComputedStyle(element,"");
if (elementStyle)
return elementStyle.getPropertyValue("background-color");
}
// Return 0 if both methods failed.
return 0;
}
|
|
By kalai, On - 2008-04-30 |
|
|
|