Javascript element style

后端 未结 4 1046
名媛妹妹
名媛妹妹 2021-01-12 09:14

I\'m curious why this one

fdsfsd
.overlay{ width: 100px; height: 200px; background-color:red; }
4条回答
  •  花落未央
    2021-01-12 09:58

    The style gives access only to information which is put into elem.style. In your example style doesn’t tell anything about the margin defined in CSS. Use getComputedStyle().

    var computedStyle = getComputedStyle(document.getElementsByClassName("overlay")[0], null)
    
    alert(computedStyle.width);
    

    jsfiddle demo

提交回复
热议问题