核心思想是用getComputedStyle獲取樣式,如果沒有獲取到就判斷是不是動態創建的元素,如果是則用style獲取行內樣式。
看重點(注釋部分)代碼吧!
curCSS = function( elem, name ) { var ret, computed = window.getComputedStyle( elem, null ), style = elem.style; if ( computed ) { //getPropertyValue兼容ie9獲取filter:Alpha(opacity=50) ret = computed.getPropertyValue( name ) || computed[ name ]; //如果是動態創建的元素,則使用style方法獲取樣式 if ( ret === "" && !jQuery.contains( elem.ownerDocument, elem ) ) { ret = jQuery.style( elem, name ); } ................. } return ret; };