Unable to get value of margin property from result getComputedStyle

前端 未结 4 1984
慢半拍i
慢半拍i 2020-12-05 00:37

The result of a getComputedStyle contains a property named \"margin\", but the property is always an empty string (\"\") in Mozilla Firefox or Appl

4条回答
  •  Happy的楠姐
    2020-12-05 01:13

    var elem = document.getElementById("your-div");
    if (elem.currentStyle) {
        var margin = elem.currentStyle.margin;
    } else if (window.getComputedStyle) {
        var margin = window.getComputedStyle(elem, null).getPropertyValue("margin");
    }
    
    alert(margin);
    

    you can use this shim,works for all browsers:refer this

    use currentStyle for Internet Explorer.

    use getComputedStyle for other browsers

提交回复
热议问题