js动态调用css属性的小规律及实例说明
作者:互联网
2025-08-18
刚才看到一篇好的文章介绍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。
给个例子让大家好理解:
复制代码代码如下:
< divonclick="alert(this.style.float);
this.style.float='left';
alert(this.style.float);" >测试1< /div >
< divonclick="alert(this.style.float);
if(this.style.cssFloat){this.style.cssFloat='left';
}else{this.style.styleFloat='left';
}alert(this.style.float);" >测试2< /div >
相关标签:
相关推荐
专题
+ 收藏
+ 收藏
+ 收藏
+ 收藏
+ 收藏
+ 收藏
最新数据
相关文章
vue3 数据响应式遇到的问题
vxe-table 自定义数字行主键,解决默认字符串主键与后端类型不匹配问题
AI 打字跟随优化
你的 Vue 3 defineEmits(),VuReact 会编译成什么样的 React?
Vue3 + TS 企业级工程化项目全套实战(Vue3 + Vite + Pinia + VueRouter + Element Plus)
Vue 3 defineOptions 宏,用 VuReact 编译成 React 长什么样?
Vue3 KeepAlive 深度揭秘:组件缓存的魔法是如何实现的?
你的 Vue 3 useAttrs(),VuReact 会编译成什么样的 React?
虚拟 DOM 的 Diff 算法:Vue/React 如何实现高效更新
前端必看!console 调试不只有 log,这 8 个技巧省一半调试时间
AI精选
