码迷,mamicode.com
首页 > Web开发 > 详细

js获取css

时间:2014-10-03 18:35:24      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   ar   strong   文件   sp   

原帖地址: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"));

 

js获取css

标签:style   blog   http   color   io   ar   strong   文件   sp   

原文地址:http://www.cnblogs.com/lanhj/p/4005008.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!