zoukankan      html  css  js  c++  java
  • JavaScript获取最终的CSS样式属性值

           如果没有在页面标签的style里面显示的设置属性值,那么使用obj.style.属性 会获取不到值,这时候如果需要获取默认值的时候则需要下面的代码帮助获取,以下对IE和其他浏览器做了不同的处理。

    /** OBJ 需要获取属性的元素,prop 属性名 **/
    function GetCurrentStyle(obj, prop){
        if (obj.currentStyle) //IE
        {
            return obj.currentStyle[prop];
        }
        else 
            if (window.getComputedStyle) //非IE
            {
                propprop = prop.replace(/([A-Z])/g, "-$1");
                propprop = prop.toLowerCase();
                return document.defaultView.getComputedStyle(obj, null)[propprop];
            }
        return null;
    }
    

    如果要得到元素的最终css样式值:可以通过下面这个通用函数得到:

    /** 得到元素的最终css样式值 **/
    function getStyle(elem,name)
    {
        if(elem.style[name])
            return elem.style[name];
        else if(elem.currentStyle)
        {
            return elem.currentStyle[name];
            
        }
        else if(document.defaultView&&document.defaultView.getComputedStyle)
        {
            name=name.replace(/[A-Z]/g,"-$1");
            name=name.toLowerCase();
            var s=document.defaultView.getComputedStyle(elem,"");
            return s&&s.getPropertyValue(name);
            
        }
        else{
            return null;
        }
    }    
    

      

    缘来天注定,缘去人自夺。种如是因,收如是果,一切唯心造。笑言面对,不去埋怨。悠然、随心、随性、随缘。
  • 相关阅读:
    P1312 [NOIP2011 提高组] Mayan 游戏
    Codeforces Round 736
    CF487E Tourists
    荏苒
    数论
    [NOI2009] 二叉查找树 题解
    元素
    线性基
    杂录
    SQL中关于Join、Inner Join、Left Join、Right Join、Full Join、On、 Where区别
  • 原文地址:https://www.cnblogs.com/gaojianqi/p/3418932.html
Copyright © 2011-2022 走看看