style、 currentStyle、 runtimeStyle区别分析
style、 currentStyle、 runtimeStyle区别分析,需要的朋友可以参考下。
1、obj.style只能获得内嵌样式(inline Style)就是写在Tag里面的,他访问不到那些链接的外部css和在head中用<style>声明的style。所以必须认识到在那些使用外部Css文件的页面中,如果用style赋值,如obj.Content-Type" content="text/html; charset=iso-8859-1">
<title>testStyle</title>
<style>
.lala{
color:yellow;
}
</style>
</head>
<body>
<div id="tt" class="lala">1111</div>
</body>
<script>
alert(document.getElementById("tt").currentStyle.color);
</script>
</html>
若去掉以上<div>中的style为<div id="tt" class="lala">1111</div>,那么currentStyle.color就根据优先级变成了yellow,但是此时style.color为空。
3、runtimeStyle简单的说就是你可以对一个节点的样式赋值,他将成为最高优先级的节点样式。
如:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style>
.lala{
color:yellow;
}</style>
</head>
<body>
<div id="tt" class="lala">1111</div>
</body>
<script>
document.getElementById("tt").runtimeStyle.color="black";
alert(document.getElementById("tt").currentStyle.color);
alert(document.getElementById("tt").runtimeStyle.color);
alert(document.getElementById("tt").style.color);
</script>
</html>
此时页面显示字的颜色是runtimeStyle赋值后的black。但是只有currentStyle.color和runtimeStyle本身能够取到这个值,style.color取到的依然是tag中的blue。
经验分享互联网动态
更多阅读推荐