js获取引用的css样式,js获取css样式方法

一、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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值