currentStyle、getComputedStyle
来源:互联网 发布:youtube未连接到网络 编辑:程序博客网 时间:2024/04/28 05:54
周末写了个原生的animation组件,其中使用原生的document.getElementById('...').style来获取元素的相关样式值,但是奇怪的是获取不到相应的值:
style只能获取元素的内联样式,内部样式和外部样式使用style是获取不到的。
currentStyle可以弥补style的不足,但是只适用于IE。
getComputedStyle同currentStyle作用相同,但是适用于FF、opera、safari、chrome。
写了个getStyle的自定义函数,来兼容ie和其他浏览器,使用getStyle来获取页面中元素的样式,问题解决:
currentStyle和getComputedStyle只能用于获取页面元素的样式,不能用来设置相关值。
如果要设置相应值,应使用style。
body{margin:0 auto;text-align:center;}div{position:relative;left:10px;}var dom1 = document.getElementById('pic1');console.log(dom1.style.left);
控制台中显示为空。
style只能获取元素的内联样式,内部样式和外部样式使用style是获取不到的。
currentStyle可以弥补style的不足,但是只适用于IE。
getComputedStyle同currentStyle作用相同,但是适用于FF、opera、safari、chrome。
写了个getStyle的自定义函数,来兼容ie和其他浏览器,使用getStyle来获取页面中元素的样式,问题解决:
function getElementStyle(el,attr){//获取el当前的attr样式,解决ie问题return el.currentStyle?el.currentStyle[attr]:getComputedStyle(el,null)[attr];//或getComputedStyle(el,null).getProperty(attr) }
获取后返回10px。
currentStyle和getComputedStyle只能用于获取页面元素的样式,不能用来设置相关值。
如果要设置相应值,应使用style。
0 0
- currentStyle、getComputedStyle
- getComputedStyle && currentStyle
- getComputedStyle、currentStyle 和getPropertyValue
- currentStyle和getComputedStyle
- getComputedStyle与currentStyle
- currentstyle和getComputedStyle兼容问题
- style currentStyle getComputedStyle
- getComputedStyle与currentStyle
- 获取样式currentstyle(),getComputedStyle()
- JS getComputedStyle() 与 currentStyle
- getcomputedstyle、currentStyle、style
- currentStyle,runtimeStyle,getComputedStyle style
- currentStyle、getComputedStyle区别
- javascript getComputedStyle,getPropertyValue,CurrentStyle说明
- style、currentStyle、getComputedStyle区别介绍
- currentStyle与getComputedStyle的用法
- style、currentStyle、getComputedStyle区别介绍
- style、currentStyle、getComputedStyle区别介绍
- Velocity的基本使用
- Java基础重温之线程——创建线程
- uva 11823 - Two Longest Paths (最小费用流)
- java杂记
- 黑马程序员----------面向对象三大特性之多态
- currentStyle、getComputedStyle
- hive原生和复合类型的数据加载和使用
- 天天写日记争当文艺青年 2014-5-18
- 五四青年节习近平在北京大学师生座谈会上的讲话
- 生意难做 淘宝客会有很多人死在2014!
- 数字格式化异常
- Linux强制用户下线
- 需求用例分析之三:补充规约
- MySQL 是否支持分区功能查询