如何在Javascript中获取CSS类属性?

测试{
宽度:80px;
高度:50px;
背景色:#808080;
利润率:20px;
}

HTML-

<div class=“测试”>单击此处&lt/部门>

在JavaScript中,我想获得margin:20px

对于现代浏览器,您可以使用getComputedStyle

变量元素,
风格
elem=document.querySelector('.test');
style=getComputedStyle(elem);
style.marginTop//`20px`
风格。边缘光//`20px`
style.marginBottom//`20px`
style.marginLeft//`20px`

margin是一种复合样式,不可靠的跨浏览器。应分别访问-top-right-bottom-left

拨弄

发表评论