js操作css属性值

 

获取css样式表的属性:

1)ie下获取对应标签css样式表属性值

document.getElementById('id').currentStyle.属性名字;

2)Firefox下

window.getComputedStyle(document.getElementById('id').,null).属性名字;

 

使用js操作css属性的写法:和css中的写法不同:

1、对于没有中划线的css属性一般直接使用style.属性名即可。

如:obj.style.margin,obj.style.width,obj.style.left,obj.style.position等。

2、对于含有中划线的css属性,将每个中划线去掉并将每个中划线后的第一个字符换成大写即可。

如:obj.style.marginTop,obj.style.borderLeftWidth,obj.style.zIndex,obj.style.fontFamily等。

这个规律我想大多数的前端开发者也都熟知。对在css中有一个特殊的属性其js使用方法确比较特殊。

因为 float 是javascript的保留字,那怎么在js中书写样式表中的 float 呢?

我们不能直接使用obj.style.float来使用,这样操作是无效的。

其正确的使用方法是为:IE用obj.style.styleFloat,其他浏览器Mozilla(gecko),ff等用obj.style.cssFloat。

例子:

<div onclick="alert(this.style.float);this.style.float='left';alert(this.style.float);">测试1</div>

<div onclick="alert(this.style.float);if(this.style.cssFloat){this.style.cssFloat='left';}else{this.style.styleFloat='left';}alert(this.style.float);">测试2</div>

javascript中css的float特殊写法

你可能感兴趣的:(JavaScript,css,前端开发,firefox,float,mozilla)