blog icon indicating copy to clipboard operation
blog copied to clipboard

currentStyle、.getComputedStyle() --> 获取活动样式(非行间样式、计算样式)

Open yongheng2016 opened this issue 8 years ago • 0 comments

  • 代码:
function getTheStyle(element, style){
  return element.currentStyle? element.currentStyle[style] : window.getComputedStyle(element, null)[style]
}  
  • 用法:
  <style>
    #mydiv {
      width: 100px;   //这里设置了div的宽度,下面要用到
      height: 100px;
      border: 10px solid yellow;
      background: red;
      color: green;
      margin: 20px;
    }
  </style>

  <div id="mydiv"></div>

  <script>

    function getTheStyle(element, style){
      return element.currentStyle? element.currentStyle[style] : window.getComputedStyle(element, null)[style]
    }  

    console.log(getTheStyle(mydiv, 'width'))  //100px

  </script>

yongheng2016 avatar Jul 24 '17 06:07 yongheng2016