js获取css

原帖地址:http://kingphp.blog.163.com/blog/static/20042324420120109438458/

 

 

我们通过dom.style获得的属性是有限制的,只能获取html中的css,而link的css是得不到的,可以这样获得:

function getStyle (obj, prop) {//获取css, 包括link的css文件中的样式
    if (obj.currentStyle) {//IE        
        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;     
}  

 

要注意一点,如果要获取带“-”的css样式,应该改成驼峰式的名字,比如:

alert(getStyle(dom, "marginTop"));

 

posted @ 2014-10-03 15:38  -六月飞雪-  阅读(190)  评论(0编辑  收藏  举报