0
点赞
收藏
分享

微信扫一扫

获取CSS样式


<!doctype html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="Yvette Lau">
<meta name="Keywords" content="关键字">
<meta name="Description" content="描述">
<title>Document</title>
<style>
#test{
width:800px;
height:300px;
background-color:#CCC;
float:left;
margin-bottom: 20px;
}
</style>
</head>
<body>
<div id = "test">
第一个DIV
CSS的样式分为三类: <br><br>
内嵌样式:是写在Tag里面的,内嵌样式只对所有的Tag有效。 <br><br>
内部样式:是写在HTML的里面的,内部样式只对所在的网页有效。 <br><br>
外部样式表:如果很多网页需要用到同样的样式(Styles),将样式(Styles)写在一个以.css为后缀的CSS文件里,然后在每个需要用到这 些样式(Styles)的网页里引用这个CSS文件。<br>

<br><p></p>getComputedStyle是一个可以获取当前元素所有最终使用的CSS属性值。返回的是一个CSS样式对象([object CSSStyleDeclaration]) </p>
<br>currentStyle是IE浏览器的一个属性,返回的是CSS样式对象
</div>
<br/>
<div id = "tag" style = "width:500px; height:700px;background-color:pink;">
第二个DIV
</div>
<script type = "text/javascript">
window.onload = function(){
var test = document.getElementById("test");
var tag = document.getElementById("tag");

//CSS样式对象:CSS2Properties{},CSSStyleDeclaration
console.log(test.style); //火狐返回空对象CSS2Properties{},谷歌返回空对象CSSStyleDeclaration{}
console.log(tag.style); //返回CSS2Properties{width:"500px",height:"300px",background-color:"pink"}
//element.style获取的是内嵌式的style,如果不是内嵌式,则是一个空对象

console.log(test.style.backgroundColor);//pink
console.log(tag.style['background-color']);//pink
//获取类似background-color,border-radius,padding-left类似样式的两种写法啊

console.log(test.currentStyle) //火狐和谷歌为Undefined,IE返回CSS对象
console.log(window.getComputedStyle(test,null))//谷歌返回CSSStyleDeclaration{……} ,火狐返回CSS2Properties{……}
console.log(window.getComputedStyle(test))
//效果同上,但是在Gecko 2.0 (Firefox 4/Thunderbird 3.3/SeaMonkey 2.1) 之前,第二个参数“伪类”是必需的(如果不是伪类,设置为null)

// console.log(test.currentStyle.width);//500px(IE)
console.log(window.getComputedStyle(test).width); //500px;
console.log(window.getComputedStyle(test)['width']);//500px;
//document.defaultView.getComputedStyle(element,null)[attr]/window.getComputedStyle(element,null)[attr]

console.log(getStyle(test,"cssFloat"));//left
console.log(getStyle(test,"float")); //left,早前FF和chrome需要使用cssFloat,不过现在已经不必
console.log(getStyle(test,"height"));//火狐和谷歌都是undefined
console.log(getStyle(tag,"height")); //IE9以下必须使用styleFloat,IE9及以上,支持styleFloat和cssFloat
console.log(getStyle(tag,"width"));
}
//对获取样式的方式进行封装
function getStyle(element, attr){
if(element.currentStyle){
return element.currentStyle[attr];//针对IE浏览器
}else{
return window.getComputedStyle(element,null)[attr];
}
}
console.log(window.getComputedStyle(test).getPropertyValue("float"))
</script>
</body>
</html>

输出效果

[Web浏览器] "left" /Test/jsTest.html (74)
[Web浏览器] "[object CSSStyleDeclaration]" /Test/jsTest.html (42)
[Web浏览器] "[object CSSStyleDeclaration]" /Test/jsTest.html (43)
[Web浏览器] "" /Test/jsTest.html (46)
[Web浏览器] "rgb(255, 192, 203)" /Test/jsTest.html (47)
[Web浏览器] "undefined" /Test/jsTest.html (50)
[Web浏览器] "[object CSSStyleDeclaration]" /Test/jsTest.html (51)
[Web浏览器] "[object CSSStyleDeclaration]" /Test/jsTest.html (52)
[Web浏览器] "800px" /Test/jsTest.html (56)
[Web浏览器] "800px" /Test/jsTest.html (57)
[Web浏览器] "left" /Test/jsTest.html (60)
[Web浏览器] "left" /Test/jsTest.html (61)
[Web浏览器] "300px" /Test/jsTest.html (62)
[Web浏览器] "700px" /Test/jsTest.html (63)
[Web浏览器] "500px" /Test/jsTest.html (64)


举报

相关推荐

0 条评论