js中style、currentStyle、getComputedStyle的区别

JS中style、currentStyle、getComputedStyle的区别:

首先样式表的引入有三种方式:内嵌样式,内部样式,外部样式表。

JS中的Style只能取得内联样式。

如:HTML中代码

<style type="text/css">

p{height:28px;}

</style>

<body>

<p id="par">This is a test.</p>

<script type="text/css">

var obj=document.getElementById("par");

var objh=obj.style.height;

alert(objh);

</script>

</body>

运行后返回警告,不会返回28px。

但当用<p style="height:30px">This is a test.</p>则会返回30px。

currentStyle只有IE支持。取得当前应用的样式。

getComputedStyle支持FF,Opera,Safari,Chrome等浏览器。取得最终应用的样式。可以这么用:

objh=document.defaultView.getComputedStyle(obj,null).height;

或:objh=window.getComputedStyle(obj,null)[height];

posted @ 2013-03-14 23:17  小小齐  阅读(215)  评论(0)    收藏  举报