JS 获取样式属性
2016-04-14 16:37
671 查看
摘要: JS获取CSS属性值方法有很多,开源昕昕这里介绍两种供大家学习
obj.style方法,这个方法只能JS只能获取写在html标签中的写在style属性中的值(style=”…”),看一下代码
<!doctype html>
<html>
<head>
<title>JS获取CSS属性值</title>
<style type=”text/css”>
<!–
.test{color:#cdcdcd;}
–>
</style>
</head>
<body>
<div id=”css88″ class=”test” style=”width:200px; height:200px; background:#333333″>JS获取CSS属性值</div>
<script type=”text/javascript”>
alert(document.getElementById(“css88”).style.width);//200px
alert(document.getElementById(“css88”).style.color);//空白
</script>
</body>
</html>
上面这个例子对id为”css88″的div设置了2种烦事的样式,包括style和外部样式class。
从alert出来的信息可以看到,document.getElementById(“css88”).style方法获取不到class为ss的属性和值。
那么这么样才能获取到class为ss的属性和值呢?
IE中使用的是obj.currentStyle方法,而FF是用的是getComputedStyle 方法。
具体案例如下:
方法一:
<!doctype html>
<html>
<head>
<title>JS获取CSS属性值</title>
<style>
#aa{height:300px;width:500px;border:solid 1px #5d5d5d;color:red;}
</style>
</head>
<body>
<div id="aa">0000</div>
<script>
function GetCurrentStyle (dd, prop) {
if (dd.currentStyle) {
return dd.currentStyle[prop];
}
else if (window.getComputedStyle) {
propprop = prop.replace (/([A-Z])/g, "-$1");
propprop = prop.toLowerCase ();
return document.defaultView.getComputedStyle (dd,null)[prop];
}
return null;
}
var dd=document.getElementById("aa");
alert(GetCurrentStyle(dd,"color"));
</script>
</body>
</html>
方法二(可将方法一简化为如下):
<script>
function getDefaultStyle(dd,attribute){
// 返回最终样式函数,兼容IE和DOM,设置参数:元素对象、样式特性
return dd.currentStyle?dd.currentStyle[attribute]:document.defaultView.getComputedStyle(dd,false)[attribute];
}
var dd=document.getElementById("aa");
alert(getDefaultStyle(dd,"color"));
</script>
obj.style方法,这个方法只能JS只能获取写在html标签中的写在style属性中的值(style=”…”),看一下代码
<!doctype html>
<html>
<head>
<title>JS获取CSS属性值</title>
<style type=”text/css”>
<!–
.test{color:#cdcdcd;}
–>
</style>
</head>
<body>
<div id=”css88″ class=”test” style=”width:200px; height:200px; background:#333333″>JS获取CSS属性值</div>
<script type=”text/javascript”>
alert(document.getElementById(“css88”).style.width);//200px
alert(document.getElementById(“css88”).style.color);//空白
</script>
</body>
</html>
上面这个例子对id为”css88″的div设置了2种烦事的样式,包括style和外部样式class。
从alert出来的信息可以看到,document.getElementById(“css88”).style方法获取不到class为ss的属性和值。
那么这么样才能获取到class为ss的属性和值呢?
IE中使用的是obj.currentStyle方法,而FF是用的是getComputedStyle 方法。
具体案例如下:
方法一:
<!doctype html>
<html>
<head>
<title>JS获取CSS属性值</title>
<style>
#aa{height:300px;width:500px;border:solid 1px #5d5d5d;color:red;}
</style>
</head>
<body>
<div id="aa">0000</div>
<script>
function GetCurrentStyle (dd, prop) {
if (dd.currentStyle) {
return dd.currentStyle[prop];
}
else if (window.getComputedStyle) {
propprop = prop.replace (/([A-Z])/g, "-$1");
propprop = prop.toLowerCase ();
return document.defaultView.getComputedStyle (dd,null)[prop];
}
return null;
}
var dd=document.getElementById("aa");
alert(GetCurrentStyle(dd,"color"));
</script>
</body>
</html>
方法二(可将方法一简化为如下):
<script>
function getDefaultStyle(dd,attribute){
// 返回最终样式函数,兼容IE和DOM,设置参数:元素对象、样式特性
return dd.currentStyle?dd.currentStyle[attribute]:document.defaultView.getComputedStyle(dd,false)[attribute];
}
var dd=document.getElementById("aa");
alert(getDefaultStyle(dd,"color"));
</script>
相关文章推荐
- Extjs4.0 最新最全视频教程
- Javascript中toFixed方法的改进
- 5个常见可用性错误和解决方案
- js数组实现图片轮播
- IE:获取完整的网页
- js可突破windows弹退效果代码
- DOM2非标准但却支持很好的几个属性小结
- Ruby简洁学习笔记(二):类继承、属性、类变量
- 举例讲解C#中自动实现的属性
- Delphi控件ListView的属性及使用方法详解
- 样式表CSS布局经验
- JSP脚本漏洞面面观
- 使用vbs获取雅虎汇率
- C#实现获取不同对象中名称相同属性的方法
- 使用BAT一句话命令实现快速合并JS、CSS
- js显示当前星期的起止日期的脚本
- PowerShell中Get-Date对象的常用属性和方法介绍
- Windows Powershell属性:描述对象是什么
- 详解C#中的属性和属性的使用
- 轻松学习C#的属性