针对不同浏览器获取到css文件里相关属性的两种方法分别是怎样的,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
															先看个例子 
代码如下:
<div >1111</div> 
<p>2222</p> 
<style>*{font-size:50px;}</style> 
此时 如果用 document.querySelector("p").style.fontSize 是获取不到50px 值的 而 document.querySelector("div").style.fontSize 返回的是100 
因此可以得知document.querySelector(elements).style 只针对与标签上的属性,如果在外部css 文件中的属性如何获取? 
这里介绍两个方法针对不同浏览器 
1、 obj.currentStyle 
2、window.getComputedStyle 
代码如下:
function getCurCss(id,porp){ 
var obj = document.getElementById(id); 
if (obj.currentStyle) { 
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; 
} 
getCurCss(id,"fontSize"); 
看完上述内容,你们掌握针对不同浏览器获取到css文件里相关属性的两种方法分别是怎样的的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注天达云行业资讯频道,感谢各位的阅读!