当前位置: 移动技术网 > IT编程>开发语言>JavaScript > js操作css样式,null和undefined的区别?

js操作css样式,null和undefined的区别?

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

1.js操作css的样式

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

我们没有让css和html分离

所以如果为了获取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比较性能上比较差,不在万不得已的情况下不适用

只读 可写

只读:只能获取不能修改

可写:可以修改的

null和undefined的区别

null和undefined都表示没有值

null是这个东西是天生存在的但是没赋值

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

比如

var aa=document.getelementbyid("aa")

 

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

undefined 这个对象压根就不存在的是人为定义并且没赋值

1. var a;undefined

2.div.aa undefined

元素节点的树状图

document>documentelement>body>tagname

offsetleft/offset 结合运动

滚动轮播

 

 

    

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

相关文章:

验证码:
移动技术网