您好,欢迎访问一九零五行业门户网

js动态调用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等。
因为float是javascript的保留字,那怎么在js中书写样式表中的float呢?
我们不能直接使用obj.style.float来使用,这样操作是无效的。
其正确的使用方法是为:ie:obj.style.stylefloat,其他浏览器mozilla(gecko),ff等用stylefloat: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>
其它类似信息

推荐信息