手机
当前位置:查字典教程网 >网页设计 > 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 hack:区分IE6,IE7,IE8,firefox

CSS书写规范及书写顺序的方法

IE6下图片下方有空间距的多种解决方法

css实现鼠标悬停时滑出层提示的方法

左中右3栏最先显示中栏内容的方法

chrome居中但ie不居中的解决方法

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

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

解决CSS浏览器兼容技巧

div+css垂直居中的五种实现方法

精品推荐
分类导航