js擷取css屬性

來源:互聯網
上載者:User
<head runat="server">
    <title></title>
    <style type="text/css">
        #btn1{ width: 330px;}
        #btn2{width: 400px;}
        #btn3{width: 100px;}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="button" id="btn1" value="1" onclick="returnStyle('btn1','width')" />
        <input type="button" id="btn2" value="2" onclick="FF()" />
        <input type="button" id="btn3" value="3" onclick="ie()" />
    </div>

    <script type="text/javascript">
        //ie或者ff下執行都可以
        function returnStyle(obj, styleName) {
            var myObj = typeof obj == "string" ? document.getElementById(obj) : obj;
            if (document.all) {
                alert(eval("myObj.currentStyle." + styleName));
            } else {
                alert(eval("document.defaultView.getComputedStyle(myObj,null)." + styleName));
            }
        }
        //只支援ff
        function FF() {
            alert(document.defaultView.getComputedStyle(document.getElementById('btn2'), null).width);
        }
        //只支援ie
        function ie() {
            alert(document.getElementById('btn3').currentStyle.width);
        }

    </script>

    </form>

 經過分析,在ie和ff下的用法是不同的!

 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.