zoukankan      html  css  js  c++  java
  • js 获取计算后的样式

    currentStyle 只支持IE

    getComputedStyle(obj, false)  支持其它浏览器

    1.复合样式:currentStyle取不到
    例:background、margin
    2.取默认样式
    3.只能读

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>获取计算后的样式</title>
    <style type="text/css">
    #div1{  height:100px; background:#069;}
    </style>
    <script type="text/javascript">
    
    
    //处理兼容问题
    
    function getStyle(obj, name)
    {
        if(obj.currentStyle)
        {
            return obj.currentStyle[name];      //只支持IE
        }
        else
        {
            return getComputedStyle(obj, false)[name];  //支持其它浏览器
        }
    }
    window.onload=function()
    {
        var oDiv=document.getElementById('div1');
        alert(getStyle(oDiv, 'width'));
        alert(getStyle(oDiv, 'backgroundColor'));  //注意在获取复合样式时要单独写,不能写background
    };
    </script>
    </head>
    
    <body>
    <div id="div1"></div>
    </body>
    </html>



  • 相关阅读:
    回发保留前台添加的html
    关于NBear数据访问层IDData
    使用js把数字转化成会计格式
    二次注入
    .htaccess利用与Bypass方式总结
    HTTPoxy漏洞(CVE-2016-5385)
    JAVA并行程序基础一
    队列-数组实现
    Vuex
    稀疏数组
  • 原文地址:https://www.cnblogs.com/chenyajie/p/2932224.html
Copyright © 2011-2022 走看看