JS获取CSS属性值

来源:互联网 发布:C语言的算术表达式 编辑:程序博客网 时间:2024/06/11 18:04
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JS获取CSS属性值</title>
 
<style type="text/css">
<!--  
.ss{background:blue; color:#cdcdcd; width:200px;display:block;}  
-->  
</style>  
</head>
<body>
<p id="qq" class="ss" >sdf</p>
<script type="text/javascript">  
function GetCurrentStyle (obj, prop) {     
    if (obj.currentStyle) {
        return obj.currentStyle[prop];     
    }  
    else if (window.getComputedStyle) {      
        propprop = prop.replace (/([A-Z])/g, "-$1");           
        propprop = prop.toLowerCase ();        
         return document.defaultView.getComputedStyle (obj,null)[prop];     
    }      
    return null;
}  
var dd=document.getElementById("qq");   
alert(GetCurrentStyle(dd,"width"));
alert(GetCurrentStyle(dd,"display"));
</script>
</body>  
</html>
0 0
原创粉丝点击