JS获取非行间样式及兼容问题

获取非行间样式:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>获取非行间样式</title>
    <style>
        #div1{height: 200px;width: 200px;background-color: red;}
    </style>
    <script>
        function getStyle(obj,name){
            if(obj.currentStyle){
                return obj.currentStyle[name]; //IE浏览器
            }
            else if(getComputedStyle){      //Firefox,chrome浏览器
                return getComputedStyle(obj,null)[name];
            }
        }
        window.onload = function(){
            var oDiv = document.getElementById('div1');
            alert(getStyle(oDiv,'height'));
        }

    </script>
</head>
<body>
<div id="div1"></div>
</body>
</html>

 

posted @ 2016-04-26 13:05  Web学海无涯  阅读(213)  评论(0编辑  收藏  举报