针对不同浏览器获取到css文件里相关属性的两种方法

document.querySelector(elements).style 只针对与标签上的属性,如果在外部css 文件中的属性如何获取,下面介绍两个方法针对不同浏览器,感兴趣的朋友可以参考下

先看个例子

复制代码
代码如下:

1111

2222




此时 如果用 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文件里相关属性的两种方法的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » CSS 教程