本篇文章主要介绍了 js 动态调用 CSS 属性的小规律及实例说明。需要的朋友可以过来参考下,希望对大家有所帮助
Javascript 是一种由 Netscape 的 LiveScript 发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如 Perl,遗留的速度问题,为客户提供更流畅的浏览效果。
刚才看到一篇好的文章介绍 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
来源: