一、CSS样式共有三种:内联样式(行间样式)、内部样式、外部样式(链接式和导入式)
#a{
width: 100px;
height: 100px;
}
@import url("css/style.css");
优先级:一般情况下:内联样式 >内部样式 >外部样式
特殊情况下:当外部样式放在内部样式之后,外部样式将覆盖内部样式。
#a{
width: 200px;
height: 200px;
background-color: red;
}
二、js获取css样式
1、内联样式(行间样式)的获取
function temp(){
var a=document.getElementById("a");
var aColor=a.style.backgroundColor;
var aWidth=a.style["width"];
alert(aColor+" "+aWidth);
// rgb(0,0,255) 100px
}
2、内部样式的获取
#a{
width: 200px;
height: 200px;
background-color: red;
}
function temp(){
// 非IE浏览器
var a=document.getElementById("a");
var aStyle=getComputedStyle(a);
var aColor=aStyle.backgroundColor;
var aWidth=aStyle["width"];
alert(aColor+" "+aWidth);
// rgb(255,0,0) 200px
// IE浏览器
// var a=document.getElementById("a");
// var aStyle=a.currentStyle;
// var aColor=aStyle.backgroundColor;
// var aWidth=aStyle["width"];
// alert(aColor+" "+aWidth);
// red 200px
}
3、外部样式的获取(同内部样式)
#a{
width: 300px;
height: 300px;
background-color: #4F5F6F;
}
js函数arguments与获取css样式方法
函数传参,当参数的个数不定时,可以使用arguments:表示实参集合 arguments.length=实参个数 获得css样式方法: getComputedStyle()方法---->得到的 ...
js获取css属性方法
function getDefaultStyle(obj,attribute){ return obj.currentStyle?obj.currentStyle[attribute] : docum ...
js获取css样式封装
封装 function getStyle(obj , attr){ return obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(ob ...
JS获取最终样式
在使用jqery时,操作什么都很方便,比如获取CSS样式,直接.css加样式名就可以获取你要的,但是JS,就麻烦点,因为有兼容问题,要做兼容,而jqery都是做好了的, 下面就是使用JS获取CSS样式 ...
js之如何获取css样式
js之如何获取css样式 一.获取内联样式 1