针对不同浏览器获取到css文件里相关属性的两种方法_ Div+Css教程-查字典教程网
针对不同浏览器获取到css文件里相关属性的两种方法
针对不同浏览器获取到css文件里相关属性的两种方法
发布时间:2016-12-27 来源:查字典编辑
摘要:先看个例子11112222*{font-size:50px;}此时如果用document.querySelector("p").style....

先看个例子

<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");

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新 Div+Css教程学习
    热门 Div+Css教程学习
    网页设计子分类