js 获取计算后的样式写法及注意事项

时间:2013-02-25    点击:82   
currentStyle
1.复合样式:currentStyle取不到
例:background、margin
2.取默认样式
3.只能读
复制代码 代码如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>获取计算后的样式</title>
<style type="text/css">
#div1{ height:100px; background:#069;}
</style>
<script type="text/javascript">
//兼容
function getStyle(obj, name)
{
if(obj.currentStyle)
{
return obj.currentStyle[name];
}
else
{
return getComputedStyle(obj, false)[name];
}
}
window.onload=function()
{
var oDiv=document.getElementById('div1');
alert(getStyle(oDiv, 'width'));
alert(getStyle(oDiv, 'backgroundColor')); //注意在获取复合样式时要单独写,不能写background
};
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>
JavaScript对象创建及继承原理实例解剖
jquery显示和隐藏div特效实例
JS等比例缩小图片尺寸的实例
innerText和textContent对比及使用介绍
js批量设置样式的三种方法不推荐使用with
> 返回     
地址:上海市普陀区胶州路941号长久商务中心 电话: QQ:
© Copyright 2012 上海网络 Product All Rights Reserved