JS获取CSS属性值
2016-10-24 15:05
465 查看
[html] view
plain copy
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title> JS获取CSS属性 </title>
<style type="text/css">
#f{background-color:#FF0000;}
</style>
</head>
<body>
<div id="f" style="width:200px; height:200px;"></div>
<script type="text/javascript">
var o = document.getElementById('f');
document.write(o.style.width + '<br>'); // 200px
document.write(o.style.backgroundColor + '<br>'); // 空白
function getDefaultStyle(obj,attribute){
return obj.style?obj.style[attribute] : document.defaultView.getComputedStyle(obj,false)[attribute];
}
document.write(getDefaultStyle(o, 'width') + '<br>'); // 200px
document.write(getDefaultStyle(o, 'backgroundColor')); // #FF0000
</script>
</body>
</html>
JS獲取CSS屬性方法:
[javascript] view
plain copy
function getDefaultStyle(obj,attribute){
return obj.currentStyle?obj.currentStyle[attribute] : document.defaultView.getComputedStyle(obj,false)[attribute];
}
plain copy
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title> JS获取CSS属性 </title>
<style type="text/css">
#f{background-color:#FF0000;}
</style>
</head>
<body>
<div id="f" style="width:200px; height:200px;"></div>
<script type="text/javascript">
var o = document.getElementById('f');
document.write(o.style.width + '<br>'); // 200px
document.write(o.style.backgroundColor + '<br>'); // 空白
function getDefaultStyle(obj,attribute){
return obj.style?obj.style[attribute] : document.defaultView.getComputedStyle(obj,false)[attribute];
}
document.write(getDefaultStyle(o, 'width') + '<br>'); // 200px
document.write(getDefaultStyle(o, 'backgroundColor')); // #FF0000
</script>
</body>
</html>
JS獲取CSS屬性方法:
[javascript] view
plain copy
function getDefaultStyle(obj,attribute){
return obj.currentStyle?obj.currentStyle[attribute] : document.defaultView.getComputedStyle(obj,false)[attribute];
}
相关文章推荐
- JS获取CSS属性值方法
- JS获取CSS属性值
- js获取、设置元素css属性值
- JS使用getComputedStyle()方法获取CSS属性值
- js获取某元素的class里面的css属性值代码
- JS获取CSS属性值
- JS使用getComputedStyle()方法获取CSS属性值
- js获取css属性值
- JS获取CSS属性值
- JS获取CSS属性值
- JS获取CSS属性值:getComputedStyle()
- 10-10 arguments存储实参、解决js获取CSS属性值兼容性、object对象、for语句遍历对象内容、数组中的方法
- JS获取CSS属性值
- js获取某元素的class里面的css属性值
- Js 获取行间样式属性值 和 css 内嵌样式或样式表中属性值的方法
- JS获取CSS属性值
- js获取css属性值
- JS基础篇--JS中使用document.defaultView.getComputedStyle()、currentStyle()方法获取CSS属性值
- JS获取对象的某个CSS属性值的问题
- 转载 JS获取CSS属性值