2011-12-24 54 views
2

我想獲得應用於元素的所有樣式,例如您在右上角看到的名爲「計算樣式」的Chrome開發人員工具中,我想要獲取所有列表簡單的方式讓所有的名單和財產獲取元素的所有計算樣式

Source Code

我想這JavaScript,但它不是我想要的,我必須手動寫CSS屬性

我只是想應用到所有樣式元素更早或默認

+0

http://stackoverflow.com/questions/754607/can-jquery-get-all-css-styles-associated-with-an-element – 2011-12-24 17:22:08

+0

的可能重複我沒有得到任何解決方案,所以我創建了我自己的 – mikul 2011-12-24 17:24:40

+0

看起來像這樣回答你的問題:http://stackoverflow.com/a/5830517/522877 – Wex 2011-12-24 18:03:32

回答

1

嘗試使用此功能(updated your jsFiddle);

function getComputedStyle(elm, style) { 
    var computedStyle; 
    if (typeof elm.currentStyle != "undefined") { 
     computedStyle = elm.currentStyle; 
    } 
    else { 
     computedStyle = document.defaultView.getComputedStyle(elm, null); 
    } 
    return computedStyle[style]; 
} 

getComputedStyle()功能是從I does Javascript!

+1

jQuery'css'函數也實現了這個功能,包括更多的「鉤子」來處理瀏覽器的不一致問題。 – 2011-12-24 18:12:49