js-style,currentStyle与getComputedStyle()
来源:互联网 发布:o2o网络借贷模式的特点 编辑:程序博客网 时间:2024/05/22 12:24
一,区别
style专指行间样式,即标签带style属性,例如:
<script> window.onload=function(){ var oDiv=document.getElementById("div1"); //alert("行间样式测试:"+oDiv.style.backgroundColor); } </script><div id="div1" style="width: 200px;height: 100px;background: red"> 这是行间样式的测试</div>
currentStyle指全局样式表、内嵌样式和 HTML 标签属性中指定的对象格式和样式的所有样式,例如以下行间和非行间样式都为currentStyle范围:
<html><head lang="en"> <style> /*非行间样式*/ #div1 border:1px solid #ccc; } </style> <script> window.onload=function(){ var oDiv=document.getElementById("div1"); //currentStyle能取行间和非行间样式 alert("测试:"+oDiv.currentStyle.backgroundColor+"---"+oDiv.currentStyle.border) } </script> </head><body><div id="div1" style="width: 200px;height: 100px;background: red"> 本div中指定的style中样式为行间样式</div>
但是currentStyle谷歌和火狐浏览器可能不兼容。解决办法是:使用
getComputedStyle()。
Dom中getComputedStyle方法可用来获取元素中所有可用的css属性列表,以数组形式返回,并且是readonly的。IE中则用currentStyle代替。
语法:arr_style=window.getComputedStyle(elem_id,ov)
其中ov:伪元素,是否要获取伪元素属性值。如hover,active,link等属性。如果不想获取这些伪元素的属性值请填写为null。返回值类型和style一样。
一,兼容性解决
//非行间样式的兼容性解决示例 if(oDiv.currentStyle){ //针对IE alert(oDiv.currentStyle.width) }else{ //针对火狐 alert(getComputedStyle(oDiv,null).width) } //封装 function getStyle(obj,name){ if(obj.currentStyle){ //针对IE return obj.currentStyle[name]; }else{ //针对火狐 return getComputedStyle(obj,null)[name] } }
0 0
- js-style,currentStyle与getComputedStyle()
- JS getComputedStyle() 与 currentStyle
- Js中的style,currentStyle,getComputedStyle()区别
- JS中style、currentStyle、getComputedStyle的区别
- JS中style、currentStyle、getComputedStyle的区别
- getComputedStyle与currentStyle获取样式(style/class)
- getComputedStyle与currentStyle获取样式(style/class)
- getComputedStyle与currentStyle获取样式(style/class)
- style currentStyle getComputedStyle
- getcomputedstyle、currentStyle、style
- currentStyle,runtimeStyle,getComputedStyle style
- js中style,currentStyle和getComputedStyle的区别
- js获取css值的方法:style、getComputedStyle和currentStyle
- JS获取CSS样式(style/getComputedStyle/currentStyle)
- js中style,currentStyle和getComputedStyle的区别
- js获取css值的方法:style、getComputedStyle和currentStyle
- js获取css值的方法:style、getComputedStyle和currentStyle
- style、currentStyle、getComputedStyle区别介绍
- iOS常用技术 - 基础动画(水波动画)
- 设计模式之工厂方法模式
- Java泛型理解
- Linux使用技巧12--Ubuntu代理
- 使用NPOI导出excel
- js-style,currentStyle与getComputedStyle()
- Android官方MVP项目单元测试
- addressof
- Solr使用:2.Solr核的创建
- JDBC非万能模板 & arraylist泛型数组 及 初始化 & 时间测试 & 通配符Insert
- Intent 关闭栈中activity 到指定activity
- 【并查集判环】HDU1272小希的迷宫【判环模板】
- 性能测试工具
- 超好的数据结构算法可视化网站