Javascript: obter valores CSS digitados, NÃO valores computados
Obviamente, isso pode ser feito desde o Firebug, mas eu não tinha certeza se eles estavam processando muito as CSSDeclarations ou se havia algo no DOM que estava faltando, mas gostaria de usar o estilo TYPED de um elemento ou folha de estilo e não o cssText que o DOM parece estar retornando.
Um exemplo seria a fronteira. Se meu elemento tiver borda: 1px sólido # 000, o DOM me devolverá
border-top-width:1px;
border-right-width-value:;
border-right-width-ltr-source:;
border-right-width-rtl-source:;
border-bottom-width:1px;
border-left-width-value:;
etc.....
Tudo o que eu realmente quero de volta é o que digitei, que era a borda: 1px sólido # 000.
Se alguém tiver alguma opinião a esse respeito, seria apreciado.
Aqui estão as especificações do DOM2 para CSS:http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSRule É aí que não tenho certeza se estou perdendo alguma coisa, se devo procurar em outro lugar.
Aqui está o código que escrevi, parece funcionar bem, mas, como eu disse, agora está devolvendo apenas os estilos renderizados pelo navegador e os estilos computados. NÃO É NECESSÁRIO olhar o código. Eu estava apenas procurando sugestões em geral. Acabei de publicar um código para ajudar alguém se ele estivesse procurando algo para começar ...
bg.fn.cssNavigator = function(){
var el = bg(this)[0]; //Only take first element.
var context = bg(this).context; //What document are we looking at?
if(!document.getElementById('plugins-bg_css_navigator-wrapper')){
jQuery("body").append('<div id="plugins-bg_css_navigator-wrapper"><div id="plugins-bg_css_navigator-css"></div></div>');
}
var t = '';
t = t+'<div>Inline Style</div><div>';
if(el.style){
var els = el.style;
for(var i=0; i<els.length; i++){
var s = els[i];
t = t+s+':'
t = t+window.getComputedStyle(el, null).getPropertyValue(s)+';<br />'; }
}
t = t+'</div>';
t = t+'<div>Computed Style</div><div>';
var cs = window.getComputedStyle(el, null);
for(var i = 0; i<cs.length; i++){
//if(typeof cs[i] === "function"){ break; }
t = t+cs[i]+':'+cs.getPropertyValue(cs[i])+'<br />';
}
t = t+'</div>';
var ssc = context.styleSheets;
for( var i in ssc ){
var isTab = false;
if(undefined !== jQuery(ssc[i].ownerNode).attr("href")){
t = t+'<div>'+jQuery(ssc[i].ownerNode).attr("href")+'</div>';
isTab = true;
}else if(undefined !== ssc[i].ownerNode){
t = t+'<div>Current File</div>';
isTab = true;
}
if(isTab){
t = t+'<div stylesheet="'+i+'">';
try{
var sscr = ssc[i].cssRules;
for( var j in sscr ){
if(undefined !== ssc[i].cssRules[j].cssText){
t = t+ssc[i].cssRules[j].cssText+'<br />';
}
}
//If we get an error, then all the stylesheets are not loaded, let's exit and try again in 100 milliseconds
}catch(e){ setTimeout( function(){ bg(el, context).cssNavigator(); }, 100 ); return false; }
t = t+'</div>';
}
}
jQuery("#plugins-bg_css_navigator-css").html(t);
};
EDITAR ########################### Na verdade, eu estava enganado sobre o Firebug. Parece que o plug-in real do Firefox parece fazer um trabalho melhor ao lidar com essas coisas, mas se você estiver usando o Firebug Lite, basta obter os estilos renderizados.