js用currentStyle和getComputedStyle获取css样式(非行间)
2012-07-31 17:47
447 查看
用js的style属性可以获得html标签的样式,但是不能获取非行间样式。那么怎么用js获取css的非行间样式呢?在IE下可以用currentStyle,而在火狐下面我们需要用到getComputedStyle。下面是一个小示例:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>js用currentStyle和getComputedStyle获取css样式</title> <style type="text/css"> #div1{width:100px; height:100px; background:red;} </style> <script type="text/javascript"> function getStyle(obj, attr) { if(obj.currentStyle) { return obj.currentStyle[attr]; } else { return getComputedStyle(obj,false)[attr]; } } window.onload=function() { var oDiv=document.getElementById('div1'); alert(getStyle(oDiv,'width')) } </script> </head> <body> <div id="div1"></div> </body> </html>
相关文章推荐
- js用currentStyle和getComputedStyle获取css样式(非行间)
- (转) js用currentStyle和getComputedStyle获取css样式(非行间)
- 对于用js获取元素的css样式属性(getComputedStyle,currentStyle,style)(getPropertyValue,getAttribute)
- js中获取元素属性、currentStyle与getComputedStyle的区别
- js获取css值的方法:style、getComputedStyle和currentStyle
- JS基础篇--JS中使用document.defaultView.getComputedStyle()、currentStyle()方法获取CSS属性值
- 如何用js获取元素样式(get.ComputedStyle()和currentStyle方法)
- JS获取CSS样式(style/getComputedStyle/currentStyle)
- js获取样式、currentStyle和getComputedStyle的兼容写法
- JS获取CSS样式(style/getComputedStyle/currentStyle)
- JS获取元素属性、样式getComputedStyle()和currentStyle方法兼容性问题
- 用JS查看修改CSS样式(cssText,attribute('style'),currentStyle,getComputedStyle)
- js获取css值的方法:style、getComputedStyle和currentStyle
- 用JS查看修改CSS样式(cssText,attribute('style'),currentStyle,getComputedStyle)
- 用currentStyle和getComputedStyle来获取CSS非行间元素
- js中style,currentStyle和getComputedStyle的区别以及获取css操作方法
- JS获取CSS样式(style/getComputedStyle/currentStyle)
- js获取css值的方法:style、getComputedStyle和currentStyle
- js用obj.currentStyle和getComputedStyle获取非行间样式的说明