2009-09-15 7 views
0

je peux obtenir un style d'un élément faisant ceci:obtenir uniquement les styles spécifiés d'un élément :)

alert (document.defaultView.getComputedStyle (document.getElementById ("element"), null).getPropertyValue ("background-color")); 

je peux obtenir tous les styles d'un élément faisant ceci:

var styles = document.defaultView.getComputedStyle (document.getElementById ("element"), null); 
var string = "" 

for (var i = 0; i < styles.length; i ++) { 
    string = string + styles[i] + ": " + styles.getPropertyValue (styles[i]) + "\n"; 
} 

alert (string); 

Mais, comment puis-je obtenir seulement les styles spécifiés d'un élément?

+0

Oui, nous pouvons probablement vous aider. – SLaks

Répondre

0

Qu'entendez-vous par «styles spécifiés»? Voulez-vous dire:

var el = document.getElementById("element"); 
alert(el.style.display); 
alert(el.style.background); 
...etc... 

ou voulez-vous dire quelque chose comme ceci:

function in_array (needle, haystack, argStrict) { 
    var key = '', strict = !!argStrict; 
    if (strict) { 
     for (key in haystack) { 
      if (haystack[key] === needle) { 
       return true; 
      } 
     } 
    } else { 
     for (key in haystack) { 
      if (haystack[key] == needle) { 
       return true; 
      } 
     } 
    } 
    return false; 
} 

var styles = document.defaultView.getComputedStyle (document.getElementById ("element"), null); 
var string = ""; 
var whichStyles = ['display','background-color']; 

for (var i = 0; i < styles.length; i ++) { 
    if(in_array(styles[i], whichStyles) { 
     string = string + styles[i] + ": " + styles.getPropertyValue (styles[i]) + "\n"; 
    } 
} 
alert (string); 
Questions connexes