针对不同浏览器获取到css文件里相关属性的两种
网站建设 2023-01-28 21:39www.1681989.com免费网站
先看个例子
<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、wdow.getComputedStyle
function getCurCss(id,porp){
var obj = document.getElementById(id);
if (obj.currentStyle) {
return obj.currentStyle[prop];
} else if (wdow.getComputedStyle) {
propprop = prop.replace(/([A-Z])/g, "-$1");
propprop = prop.toLowerCase();
return document.defaultView.getComputedStyle(obj, null)[prop];
}
return null;
}
getCurCss(id,"fontSize");
复制代码
代码如下:<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、wdow.getComputedStyle
复制代码
代码如下:function getCurCss(id,porp){
var obj = document.getElementById(id);
if (obj.currentStyle) {
return obj.currentStyle[prop];
} else if (wdow.getComputedStyle) {
propprop = prop.replace(/([A-Z])/g, "-$1");
propprop = prop.toLowerCase();
return document.defaultView.getComputedStyle(obj, null)[prop];
}
return null;
}
getCurCss(id,"fontSize");
网站设计
- 静宁会SEO的网站建设公司:全面提升您的网络影
- 提升在线业务的关键:选择最佳的丽水网站建设
- 浙江网站优化发展潜力如何
- 井研专业的网站建设公司:打造您的在线品牌
- 灵山SEO网站建设公司:提升您的在线业务表现
- 蒙城网站建设优化公司:提升您网站表现的理想
- 阳谷企业网站优化:提升线上业务力的关键
- 樟树专业的网站建设公司:打造您在线业务的坚
- 通河百度SEO排名的策略与技巧
- 重庆百度快照排名如何进行精准的客户引流
- 重庆百度快照排名
- 常宁便宜的建站公司:助您轻松打造在线业务
- 巫溪百度网站优化:提升网站曝光率与流量的关
- 湖北整站优化怎么做才能放大客户需求
- 闸北网站建设多少钱?全面解析与预算规划
- 辽宁企业网站优化怎么做电话营销