微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JS获取CSS样式style/getComputedStyle/currentStyle

CSS的样式分为三类: 内嵌样式:是写在Tag里面的,内嵌样式只对所有的Tag有效。 内部样式:是写在HTML的里面的,内部样式只对所在的网页有效。 外部样式表:如果很多网页需要用到同样的样式(Styles),将样式(Styles)写在一个以.css为后缀的CSS文件里,然后在每个需要用到这 些样式(Styles)的网页里引用这个CSS文件

getComputedStyle

一个可以获取当前元素所有最终使用的CSS属性值。返回的是一个CSS样式对象([object CSsstyleDeclaration])

currentStyle

是IE浏览器的一个属性,返回的是CSS样式对象

element指JS获取的DOM对象 element.style //只能获取内嵌样式 element.currentStyle //IE浏览器获取非内嵌样式 window.getComputedStyle(element,伪类) //非IE浏览器获取非内嵌样式 document.defaultview.getComputedStyle(element,伪类)//非IE浏览器获取非内嵌样式

注:

Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1) 之前,第二个参数“伪类”是必需的(如果不是伪类,设置为null),现在可以省略这个参数。

下面的html中包含两种css样式,id为tag的div是内嵌样式,而id为test的div样式为内部样式.

rush:xhtml;"> <Meta charset="UTF-8"> <Meta name="Generator" content="EditPlus®"> <Meta name="Author" content="Yvette Lau"> <Meta name="Keywords" content="关键字"> <Meta name="Description" content="描述"> Document

JS部分

<div class="jb51code">
<pre class="brush:js;">
<script type = "text/javascript">
window.onload = function(){
var test = document.getElementById("test");
var tag = document.getElementById("tag");

//CSS样式对象:CSS2Properties{},CS<a href="https://www.jb51.cc/tag/sst/" target="_blank" class="keywords">sst</a>yleDecl<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>tion
console.log(test.style); //火狐返回空对象CSS2Properties{},谷歌返回空对象CS<a href="https://www.jb51.cc/tag/sst/" target="_blank" class="keywords">sst</a>yleDecl<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>tion{} 
console.log(tag.style); //返回CSS2Properties{width:"500px",height:"300px",background-color:"pink"}
//element.style<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>的是内嵌式的style,如果不是内嵌式,则是<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>空对象

console.log(tag.style.backgroundColor);//pink
console.log(tag.style['background-color']);//pink
//<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>类似background-color,border-radius,padding-left类似样式的两种写法啊

console.log(test.currentStyle) //火狐和谷歌为Undefined,IE返回CSS对象
console.log(window.getCo<a href="https://www.jb51.cc/tag/mpu/" target="_blank" class="keywords">mpu</a>tedStyle(test,null))//谷歌返回CS<a href="https://www.jb51.cc/tag/sst/" target="_blank" class="keywords">sst</a>yleDecl<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>tion{……} ,火狐返回CSS2Properties{……}
console.log(window.getCo<a href="https://www.jb51.cc/tag/mpu/" target="_blank" class="keywords">mpu</a>tedStyle(test))
//<a href="https://www.jb51.cc/tag/xiaoguo/" target="_blank" class="keywords">效果</a>同上,但是在Gecko 2.0 (Firefox 4/Thunderbird 3.3/SeaMonkey 2.1) 之前,第二个参数“伪类”是必需的(如果不是伪类,设置为null)

console.log(test.currentStyle.width);//500px(IE)
console.log(window.getCo<a href="https://www.jb51.cc/tag/mpu/" target="_blank" class="keywords">mpu</a>tedStyle(test).width); //500px;
console.log(window.getCo<a href="https://www.jb51.cc/tag/mpu/" target="_blank" class="keywords">mpu</a>tedStyle(test)['width']);//500px;
//document.<a href="https://www.jb51.cc/tag/defaultview/" target="_blank" class="keywords">defaultview</a>.getCo<a href="https://www.jb51.cc/tag/mpu/" target="_blank" class="keywords">mpu</a>tedStyle(element,null)[attr]/window.getCo<a href="https://www.jb51.cc/tag/mpu/" target="_blank" class="keywords">mpu</a>tedStyle(element,null)[attr]   

}

以上的例子仅是验证前面的论述是否正确。

为了简单,我们也可以对获取样式做一个简单的封装。

rush:js;"> function getStyle(element,attr){ if(element.currentStyle){ return element.currentStyle[attr]; }else{ return window.getComputedStyle(element,null)[attr]; } } console.log(getStyle(test,"cssFloat"));//left console.log(getStyle(test,"float")); //left,早前FF和chrome需要使用cssFloat,不过现在已经不必 console.log(getStyle(test,"stylefloat"));//火狐和谷歌都是undefined console.log(getStyle(test,"styleFloat")); //IE9以下必须使用styleFloat,IE9及以上,支持styleFloat和cssFloat
console.log(window.getCo<a href="https://www.jb51.cc/tag/mpu/" target="_blank" class="keywords">mpu</a>tedStyle(test).getPropertyValue("float"))

对应float样式,IE中使用的是styleFloat,而早前的FF和chrome使用的是cssFloat,现在FF和Chrome已经支持float,还有一些其他的属性,不再一一列出,为了不去记忆这些差异点,我们引出两个访问CSS样式对象的方法: getPropertyValue方法和getAttribute方法

IE9及其它浏览器(getPropertyValue)

window.getComputedStyle(element,null).getPropertyValue(“float”); element.currentStyle.getPropertyValue(“float”); getPropertyValue不支持驼峰写法。(兼容IE9及以上,FF,Chrom,Safari,Opera) 如:window.getComputedStyle(element,null).getPropertyValue(“background-color”);

对于IE6~8,需要使用getAttribute方法,用于访问CSS样式对象的属性

element.currentStyle.getAttribute(“float”);//不再需要写成styleFloat element.currentStyle.getAttribute(“backgroundColor”);//属性名需要写成驼峰写法,否则IE6不支持,如果无视IE6,可以写成”background-color”

以上就是本文的全部内容,希望对大家的学习有所帮助,轻松使用JS获取CSS样式。

原文地址:https://www.jb51.cc/js/50490.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐