当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JS(JavaScript)的进一步了解7(更新中···)

JS(JavaScript)的进一步了解7(更新中···)

2018年12月03日  | 移动技术网IT编程  | 我要评论

1.js操作css样式

  div.style.width=”100px”.div标签内我们添加了一个style属性,并设定

  了width值。这种写法会给标签带来大量的style属性,跟实际项目是不符。

  我们没有让csshtml分离。

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

  window.getcomputedstyle() 获取经过计算机计算的所有属性

  就是只要渲染出来的都是经过计算的。

  getcomputedstyle()第一个参数是当前元素,第二个一般我们写null

  并且这个方法是只读,

  ie6-8 不支持这个用法,ie的是用currentstyle

  

2.try{

  }catch(error){} 不报错执行try里面的代码块,报错执行catch里面的代码块。

  前提条件是报错,如果不是报错不能使用

  var csss;

  try{

  csss=window.getcomputedstyle(aa,null)

  }catch(e){

  csss=aa.currentstyle

  }

  console.log(csss)

 

总结

js解决兼容的方法

  1.||

    var dd=document.documentelement.clientwidth||document.body.clientwidth

  2.if()else{}

    if(window.getcomputedstyle){

    csss=window.getcomputedstyle(aa,null)

    }else{

    csss=aa.currentstyle

    }

    console.log(csss)

    3.try{} catch(err){}

    必须在报错的条件下,和if  else比较性能上比较差,不在万不得以的情况下不使用

 

  只读 可写的区别:

 

  只读: 只能获取不能修改

 

  可写:可以修改的

 

nullundefined的区别

nullundefined都表示没有值

 

  1.null 是这个东西是天生存在的但是没给值。

 

    如果我们需要清除浏览器变量的内存需要赋值null

 

    比如  var aa=document.getelementbyid("aa")

 

    console.log(aa.parentnode.parentnode.parentnode.parentnode) null

 

   2.undefined 这个东西压根就不存在的是人为定义的并且没赋值。

    1)var a;undefined

     2)div.aa undefined

 

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网