欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript 獲取元素樣式必殺技

 更新時(shí)間:2014年05月04日 17:48:04   作者:  
這篇文章主要介紹了javascript 獲取元素樣式必殺技,需要的朋友可以參考下

Javascript獲取CSS屬性值方法:getComputedStyle和currentStyle

1 .對(duì)于元素的內(nèi)聯(lián)CSS樣式(<div style="color:#369">hello</div>),可以直接使用element.style.color來(lái)直接獲取css屬性的值;

2. 但是對(duì)于外部定義的css樣式使用這種方式就無(wú)法獲取了,而且IE瀏覽器和其他標(biāo)準(zhǔn)瀏覽器(Firefox,Chrome,Opera,Safari)使用的方法不一樣,IE瀏覽器使用element.currentStyle,W3C標(biāo)準(zhǔn)瀏覽器使用getComputedStyle來(lái)獲取。

1. IE獲取元素外部定義的CSS屬性值: element.currentStyle

currentStyle對(duì)象返回了元素上的樣式表,但是style對(duì)象只返回通過(guò)style標(biāo)簽屬性應(yīng)用到元素的內(nèi)嵌樣式。

因此,通過(guò)currentStyle對(duì)象獲取的樣式值可能與通過(guò)style對(duì)象獲取的樣式值不同。

例如,如果段落的color屬性值通過(guò)鏈接或嵌入樣式表設(shè)置為紅色( red ),而不是內(nèi)嵌的話,對(duì)象.currentStyle.color 將返回正確的顏色,而對(duì)象style.color不能返回值。但是,如果用戶指定了 <P STYLE="color:'red'">,currentStyle和STYLE對(duì)象都將返回值 red。
currentStyle對(duì)象反映了樣式表中的樣式優(yōu)先順序。在 HTML 中此順序?yàn)椋?

1) 內(nèi)嵌樣式

2) 樣式表規(guī)則

3) HTML 標(biāo)簽屬性

4) HTML 標(biāo)簽的內(nèi)部定義

2. W3C獲取元素外部定義的CSS屬性值: window.getComputedStyle(element, pseudoElt)
element必選,HTML元素
pseudoElt必選,獲取該元素的偽類樣式

復(fù)制代碼 代碼如下:

function getStyle(node, property){
if (node.style[property]) {
return node.style[property];
}
else if (node.currentStyle) {
return node.currentStyle[property];
}
else if (document.defaultView && document.defaultView.getComputedStyle) {
var style = document.defaultView.getComputedStyle(node, null);
return style.getPropertyValue(property);
}
return null;
}

相關(guān)文章

最新評(píng)論