JS使用getComputedStyle()方法獲取CSS屬性值
更新時(shí)間:2014年04月23日 15:30:45 作者:
經(jīng)常會(huì)用到j(luò)s來(lái)獲取元素的CSS樣式,由于方法眾多,在下面的文章中為大家詳細(xì)整理下
在對(duì)網(wǎng)頁(yè)進(jìn)行調(diào)試的過(guò)程中,經(jīng)常會(huì)用到j(luò)s來(lái)獲取元素的CSS樣式,方法有很多很多,現(xiàn)在僅把我經(jīng)常用的方法總結(jié)如下:
1. obj.style:這個(gè)方法只能JS只能獲取寫(xiě)在html標(biāo)簽中的寫(xiě)在style屬性中的值(style=”…”),而無(wú)法獲取定義在<style type="text/css">里面的屬性。
<span style="font-family:Arial;font-size:14px;"><!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>JS獲取CSS屬性值</title>
<style type=”text/css”>
<!–
.ss{color:#cdcdcd;}
–>
</style>
</head>
<body>
<div id=”css88″ class=”ss” style=”width:200px; height:200px; background:#333333″>JS獲取CSS屬性值</div>
<script type=”text/javascript”>
alert(document.getElementById(“css88″).style.width);//200px
alert(document.getElementById(“css88″).style.color);//空白
</script>
</body>
</html> </span>
2. IE中使用的是obj.currentStyle方法,而FF是用的是getComputedStyle 方法
“DOM2級(jí)樣式”增強(qiáng)了document.defaultView,提供了getComputedStyle()方法。這個(gè)方法接受兩個(gè)參數(shù):要取得計(jì)算樣式的元素和一個(gè)偽元素字符串(例如“:after”)。如果不需要偽元素信息,第二個(gè)參數(shù)可以是null。getComputerStyle()方法返回一個(gè)CSSStyleDeclaration對(duì)象,其中包含當(dāng)前元素的所有計(jì)算的樣式。以下面的HTML頁(yè)面為例:
<span style="font-family:Arial;font-size:14px;"><!DOCTYPE html>
<html>
<head>
<title>計(jì)算元素樣式</title>
<style>
#myDiv {
background-color:blue;
width:100px;
height:200px;
}
</style>
<body>
<div id ="myDiv" style="background-color:red; border:1px solid black"></div>
<script>
var myDiv = document.getElementById("myDiv");
var computedStyle = document.defaultView.getComputedStyle(myDiv, null);
alert(computedStyle.backgroundColor); //"red"
alert(computedStyle.width); //"100px"
alert(computedStyle.height); //"200px"
alert(computedStyle.border); //在某些瀏覽器中是“1px solid black”
</script>
</body>
</head>
</html></span>
邊框?qū)傩钥赡芤膊粫?huì)返回樣式表中實(shí)際的border規(guī)則(Opera會(huì)返回,但其它瀏覽器不會(huì))。存在這個(gè)差別的原因是不同瀏覽器解釋綜合屬性的方式不同,因?yàn)樵O(shè)置這種屬性實(shí)際上會(huì)涉及很多其他的屬性。在設(shè)置border時(shí),實(shí)際上是設(shè)置了四個(gè)邊的邊框?qū)挾?、顏色、樣式屬性。因此,即使computedStyle.border不會(huì)在所有瀏覽器中都返回值,但computedStyle.borderLeftWidth則會(huì)返回值。
需要注意的是,即使有些瀏覽器支持這種功能,但表示值的方式可能會(huì)有所區(qū)別。例如,F(xiàn)irefox和Safari會(huì)返回將所有顏色轉(zhuǎn)換成RGB格式。因此,即使getComputedStyle()方法時(shí),最好多在幾種瀏覽器中測(cè)試一下。
IE不支持getComputedStyle()方法,但它有一種類(lèi)似的概念。在IE中,每個(gè)具有style屬性的元素還有一個(gè)currentStyle屬性。這個(gè)屬性是CSSStyleDeclaration的實(shí)例,包含當(dāng)前元素全部計(jì)算后的樣式。取得這些樣式的方法差不多,如下:
<span style="font-family:Arial;font-size:14px;">var myDiv = document.getElementById("myDiv");
var computedStyle = myDiv.currentStyle;
alert(computedStyle.backgroundColor); //"red"
alert(computedStyle.width); //"100px"
alert(computedStyle.height); //"200px"
alert(computedStyle.border); //undefined</span>
與DOM版本的方式一樣,IE也沒(méi)有返回border樣式,因?yàn)檫@是一個(gè)綜合屬性。
3. 我自己在寫(xiě)測(cè)試case過(guò)程中寫(xiě)的一個(gè)簡(jiǎn)單的函數(shù)(適用于Chrome):
<span style="font-family:Arial;font-size:14px;">function getCSS(div){
return document.defaultView.getComputedStyle(div, null);
//return div.currentStyle;//沒(méi)用過(guò),IE
}</span>
1. obj.style:這個(gè)方法只能JS只能獲取寫(xiě)在html標(biāo)簽中的寫(xiě)在style屬性中的值(style=”…”),而無(wú)法獲取定義在<style type="text/css">里面的屬性。
復(fù)制代碼 代碼如下:
<span style="font-family:Arial;font-size:14px;"><!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>JS獲取CSS屬性值</title>
<style type=”text/css”>
<!–
.ss{color:#cdcdcd;}
–>
</style>
</head>
<body>
<div id=”css88″ class=”ss” style=”width:200px; height:200px; background:#333333″>JS獲取CSS屬性值</div>
<script type=”text/javascript”>
alert(document.getElementById(“css88″).style.width);//200px
alert(document.getElementById(“css88″).style.color);//空白
</script>
</body>
</html> </span>
2. IE中使用的是obj.currentStyle方法,而FF是用的是getComputedStyle 方法
“DOM2級(jí)樣式”增強(qiáng)了document.defaultView,提供了getComputedStyle()方法。這個(gè)方法接受兩個(gè)參數(shù):要取得計(jì)算樣式的元素和一個(gè)偽元素字符串(例如“:after”)。如果不需要偽元素信息,第二個(gè)參數(shù)可以是null。getComputerStyle()方法返回一個(gè)CSSStyleDeclaration對(duì)象,其中包含當(dāng)前元素的所有計(jì)算的樣式。以下面的HTML頁(yè)面為例:
復(fù)制代碼 代碼如下:
<span style="font-family:Arial;font-size:14px;"><!DOCTYPE html>
<html>
<head>
<title>計(jì)算元素樣式</title>
<style>
#myDiv {
background-color:blue;
width:100px;
height:200px;
}
</style>
<body>
<div id ="myDiv" style="background-color:red; border:1px solid black"></div>
<script>
var myDiv = document.getElementById("myDiv");
var computedStyle = document.defaultView.getComputedStyle(myDiv, null);
alert(computedStyle.backgroundColor); //"red"
alert(computedStyle.width); //"100px"
alert(computedStyle.height); //"200px"
alert(computedStyle.border); //在某些瀏覽器中是“1px solid black”
</script>
</body>
</head>
</html></span>
邊框?qū)傩钥赡芤膊粫?huì)返回樣式表中實(shí)際的border規(guī)則(Opera會(huì)返回,但其它瀏覽器不會(huì))。存在這個(gè)差別的原因是不同瀏覽器解釋綜合屬性的方式不同,因?yàn)樵O(shè)置這種屬性實(shí)際上會(huì)涉及很多其他的屬性。在設(shè)置border時(shí),實(shí)際上是設(shè)置了四個(gè)邊的邊框?qū)挾?、顏色、樣式屬性。因此,即使computedStyle.border不會(huì)在所有瀏覽器中都返回值,但computedStyle.borderLeftWidth則會(huì)返回值。
需要注意的是,即使有些瀏覽器支持這種功能,但表示值的方式可能會(huì)有所區(qū)別。例如,F(xiàn)irefox和Safari會(huì)返回將所有顏色轉(zhuǎn)換成RGB格式。因此,即使getComputedStyle()方法時(shí),最好多在幾種瀏覽器中測(cè)試一下。
IE不支持getComputedStyle()方法,但它有一種類(lèi)似的概念。在IE中,每個(gè)具有style屬性的元素還有一個(gè)currentStyle屬性。這個(gè)屬性是CSSStyleDeclaration的實(shí)例,包含當(dāng)前元素全部計(jì)算后的樣式。取得這些樣式的方法差不多,如下:
復(fù)制代碼 代碼如下:
<span style="font-family:Arial;font-size:14px;">var myDiv = document.getElementById("myDiv");
var computedStyle = myDiv.currentStyle;
alert(computedStyle.backgroundColor); //"red"
alert(computedStyle.width); //"100px"
alert(computedStyle.height); //"200px"
alert(computedStyle.border); //undefined</span>
與DOM版本的方式一樣,IE也沒(méi)有返回border樣式,因?yàn)檫@是一個(gè)綜合屬性。
3. 我自己在寫(xiě)測(cè)試case過(guò)程中寫(xiě)的一個(gè)簡(jiǎn)單的函數(shù)(適用于Chrome):
復(fù)制代碼 代碼如下:
<span style="font-family:Arial;font-size:14px;">function getCSS(div){
return document.defaultView.getComputedStyle(div, null);
//return div.currentStyle;//沒(méi)用過(guò),IE
}</span>
您可能感興趣的文章:
相關(guān)文章
三種檢測(cè)iPhone/iPad設(shè)備方向的方法
這篇文章主要介紹了檢測(cè)iPhone/iPad設(shè)備方向的三種方法,需要的朋友可以參考下2014-04-04text-align:justify實(shí)現(xiàn)文本兩端對(duì)齊 兼容IE
對(duì)于text-align 我們?cè)偈煜げ贿^(guò)了,可是它有個(gè)justify屬性,平時(shí)很少用到,就鮮為人知了。justify是一種文本靠?jī)蛇叢季址绞?,一般?yīng)用于書(shū)刊雜志排版;合理運(yùn)用text-align:justify 有時(shí)會(huì)省去很多開(kāi)發(fā)的時(shí)間,通過(guò)本文介紹text-align:justify實(shí)現(xiàn)文本兩端對(duì)齊 兼容IE2015-08-08js使用html()或text()方法獲取設(shè)置p標(biāo)簽的顯示的值
html()方法可以用來(lái)讀取或者設(shè)置某個(gè)元素中的HTML內(nèi)容,text()方法可以用來(lái)讀取或者沒(méi)置某個(gè)元素中的文本內(nèi)容2014-08-08css transform 3D幻燈片特效實(shí)現(xiàn)步驟解讀
3D幻燈片特效想必大家以不在陌生至于表現(xiàn)形式一般都是拘泥于傳統(tǒng)接下來(lái)為大家介紹下使用css3 transform配合js以及html實(shí)現(xiàn)3D幻燈片特效2013-03-03Firefox和IE瀏覽器兼容JS腳本寫(xiě)法小結(jié)
window.event兼容腳本 1.2.屏蔽Form提交事件 3.獲取事件源 4.添加事件兼容寫(xiě)法 5.Firefox注冊(cè)innerText寫(xiě)法 6.長(zhǎng)度 7.父控件下的子控件 8.XmlHttp2008-07-07js實(shí)現(xiàn)登錄注冊(cè)框手機(jī)號(hào)和驗(yàn)證碼校驗(yàn)(前端部分)
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)登錄注冊(cè)框手機(jī)號(hào)和驗(yàn)證碼校驗(yàn)的前端部分代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09詳解JavaScript對(duì)象轉(zhuǎn)原始值
這篇文章主要為大家介紹了vue組件通信的幾種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2021-12-12