in un mio vecchio sito ho usato questo set di funzioni per recuperare gli stili di un elemento:
dovrebbero essere ancora attuali.
codice:
function getCssProperty(el,styleCss){
// se invece dell'oggetto fosse passato solo l'id, allora recupero l'oggetto
if(typeof(el)=='string') {
var lista_el = document.getElementsByTagName(el);
var el = lista_el[0];
}
// funzione per converite la sintassi Css in quella usata da Javascript
function toCamelCase(s) {
for(var exp = toCamelCase.exp;
exp.test(s); s = s.replace(exp, RegExp.$1.toUpperCase()) );
return s;
}
toCamelCase.exp = /-([a-z])/;
var styleJs=toCamelCase(styleCss);
// se l'elemento avesse gia un valore inline (che predomina) restituisco quello
var value = el.style[styleCss];
if(!value)
// metodo x FF: vuole il nome della proprieta con la sintassi originale
if(document.defaultView) value = document.defaultView.getComputedStyle(el, null).getPropertyValue(styleCss);
// metodo x IE: vuole il nome della proprieta con la sintassi javascript
else if(el.currentStyle) value = el.currentStyle[styleJs];
return value;
}
function getCssPropertybyId(el,styleCss){
// se invece dell'oggetto fosse passato solo l'id, allora recupero l'oggetto
if(typeof(el)=='string') {
var el = document.getElementById(el);
}
// funzione per converite la sintassi Css in quella usata da Javascript
function toCamelCase(s) {
for(var exp = toCamelCase.exp;
exp.test(s); s = s.replace(exp, RegExp.$1.toUpperCase()) );
return s;
}
toCamelCase.exp = /-([a-z])/;
var styleJs=toCamelCase(styleCss);
// se l'elemento avesse gia un valore inline (che predomina) restituisco quello
var value = el.style[styleCss];
if(!value)
// metodo x FF: vuole il nome della proprieta con la sintassi originale
if(document.defaultView) value = document.defaultView.getComputedStyle(el, null).getPropertyValue(styleCss);
// metodo x IE: vuole il nome della proprieta con la sintassi javascript
else if(el.currentStyle) value = el.currentStyle[styleJs];
return value;
}
function getState(el,styleCss){
// se invece dell'oggetto fosse passato solo l'id, allora recupero l'oggetto
if(typeof(el)=='string') {
var el = document.getElementById(el);
}
// funzione per converite la sintassi Css in quella usata da Javascript
function toCamelCase(s) {
for(var exp = toCamelCase.exp;
exp.test(s); s = s.replace(exp, RegExp.$1.toUpperCase()) );
return s;
}
toCamelCase.exp = /-([a-z])/;
var styleJs=toCamelCase(styleCss);
// se l'elemento avesse gia un valore inline (che predomina) restituisco quello
var value = el.style[styleCss];
if(!value)
// metodo x FF: vuole il nome della proprieta con la sintassi originale
if(document.defaultView) value = document.defaultView.getComputedStyle(el, null).getPropertyValue(styleCss);
// metodo x IE: vuole il nome della proprieta con la sintassi javascript
else if(el.currentStyle) value = el.currentStyle[styleJs];
return value;
}