原生js获取外联样式

1、getComputedStyle(ele,null).fontSize //ele所需获取样式的元素节点 第二个参数填null就好。后面属性是你需要的样式属性。ie不支持。

2、ie:oBox.currentStyle.fontSize //ie内使用currentStyle

3、兼容:

  function getCurrentStyle(ele,attr){

    if(window.getComputedStyle){

      return getComputedStyle(ele,null)[attr];//[attr]的方式取属性可以使用变量,而点的方式不能。

    }else{

      return ele.currentStyle[attr];

    }

  }

猜你喜欢

转载自www.cnblogs.com/muzs/p/9634654.html