.test { width:80px; height:50px; background-color:#808080; margin:20px; }
HTML –
<div class="test">Click Here</div>
En JavaScript, je veux margin:20px
Pour les navigateurs modernes, vous pouvez utiliser getComputedStyle
:
var elem, style; elem = document.querySelector('.test'); style = getComputedStyle(elem); style.marginTop; //`20px` style.marginRight; //`20px` style.marginBottom; //`20px` style.marginLeft; //`20px`
margin
est un style composite, et un navigateur croisé non fiable. Chacun de -top
-right
, -bottom
et -left
devrait être consulté individuellement.
violon
Je viens de publier un paquet npm à cet effet exactement. Vous pouvez le trouver ici sur npm ou github:
Npm: https://www.npmjs.com/package/stylerjs
Github: https://github.com/tjcafferkey/stylerjs
Vous l'utiliseriez comme si
var styles = styler('.class-name').get(['height', 'width']);
Et les styles seraient égaux
{height: "50px", width: "50px}
Donc, vous pouvez simplement obtenir les valeurs comme
var height = styles.height;
Utilisation de jQuery:
$('.class').css( "backgroundColor" );