Code
function getStyle(elem, cssprop, cssprop2){
// IE
if (elem.currentStyle) {
return elem.currentStyle[cssprop];
// other browsers
} else if (document.defaultView && document.defaultView.getComputedStyle) {
document.defaultView.getComputedStyle(elem,null);
return r.getPropertyValue(cssprop2);
// fallback
} else {
return null;
}
}
// test
var e = document.querySelector('body');
console.log( getStyle(e) );
If you want all the styles for one element, just add a for loop.
Code
function getStyle(elem){
if (document.defaultView && document.defaultView.getComputedStyle) {
var r = document.defaultView.getComputedStyle(elem,null);
var rr = "";
for(i = 0; i < r.length; i++){
rr += '' + r[i] + ': ' + r.getPropertyValue(r[i]) + ';' + "\n";
}
return rr;
// fallback
} else {
return null;
}
}
// test
var e = document.querySelector('body');
console.log( getStyle(e) );
This doesn't exactly seem like you want though, because you are contradicting yourself with your latest post. Is it that hard to be clear? Guess so.
Why would you ever want to change the CSS document to make a CSS change? Just append for example into the style attribute: style="background-color: red !important;"
This post was edited by eagl3s1ght on Jun 7 2012 05:36am