js操作css样式、js的兼容问题

一、js操作css样式

 div . style . width="200px"  在div标签内我们添加了一个style属性,并设定width值。这种写法会给标签带来大量的style属性,跟实际项目是不符,我们没有让css和html分离

 所以如果是为了获取css样式

  window . getcomputedstyle()

  获取经过计算机的所有属性

  getcomputed style 参数

    第一个参数是当前元素,第二个一般写null ; 并且这个方法是只读的(只能读取,不能修改)

    IE6—8不支持这个写法,Ie的用currentstyle

二、js的兼容方法

  1、||(或)

    var dd=document . documentElement . clienWidth || document . body . clientWidth

  2、if()else { }

    if (window . getcomputedstyle){

      csss=window . getcomputedstyle(aa . null)

      }else{

        csss=aa . currentstyle

        }

      console . log(csss)

  3、try { } cath (err){ }

    var csss;

      try {

        csss=window . getcomputedstyle (aa . null)

      } cath (err){

        csss=aa . currentstyle

        }

    不报错,执行try里面的代码块,报错执行cath里面的代码块;前提是条件是报错,如果不是报错,就不用使用

    这必须是在报错的前提下,和if else 比较性能上比较差,不在万不得已的情况下不使用