How to get all CSS of element

后端 未结 1 1340
傲寒
傲寒 2020-12-05 00:40

I have been testing the Javascript CSS functions today and noticed that when .style.cssText is used it only gives me the CSS that i set with JS.

I instead wanted to

相关标签:
1条回答
  • 2020-12-05 01:26

    MyDiv001.style.cssText will return only inline styles, that was set by style attribute or property.

    You could use getComputedStyle to fetch all styles applied to element. You could iterate over returned object to dump all styles.

    function dumpCSSText(element){
      var s = '';
      var o = getComputedStyle(element);
      for(var i = 0; i < o.length; i++){
        s+=o[i] + ':' + o.getPropertyValue(o[i])+';';
      }
      return s;
    }
    
    0 讨论(0)
提交回复
热议问题