手机
当前位置:查字典教程网 >网页设计 > Div+Css教程 >针对不同浏览器获取到css文件里相关属性的两种方法
针对不同浏览器获取到css文件里相关属性的两种方法
摘要:先看个例子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");

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

CSS:使DIV 在浏览器窗口中水平垂直居中

IE6下div层被select控件遮住的问题解决方法

针对主流浏览器的CSS-HACK写法及IE常用条件注释

彻底清除浏览器js和css缓存的方法

Ext Grid控件的配置与方法

css中不确定高度垂直居中2种方法

关于浏览器兼容性的讨论

不同浏览器兼容的CSS编码准则

浏览器如何判断css优先级

为不同的浏览器载入不同CSS的二种方法

精品推荐
分类导航