如何获取到css文件里的相关属性

先看个例子

<div  style="font-size:100px;">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");

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值