JS获取非行内样式

最近遇到的一个问题,如何获取非行内样式,如果用普通方式console的话是拿不到数值的

需要使用currentStyle获取才可以在console台拿到数据

 var timers = document.getElementsByClassName("time-out")[0];

 if (timers.currentStyle) {
        this.conswidth = timers.currentStyle.width;
        this.consheight = timers.currentStyle.height;
      } else {
        this.conswidth = getComputedStyle(timers, null).width;
        this.consheight = getComputedStyle(timers, null).height;
      }

else为兼容IE浏览器方法,其中传的两个参数,一个为获取到的div,另一个无实际作用,直接传null即可

此时在console台打印this.conswidth就可以准确的拿到数值了

posted @ 2020-10-28 09:17  湫涑  阅读(139)  评论(0编辑  收藏  举报