这里是 jQuery 的一个独立版本的 curCSS。请注意我进行了编辑以使代码尺寸更小。到目前为止,它并没有给我带来任何问题。
var curCSS;
(function(){
var getStyles, core_pnum = /[+-]?(?:\d*\.|)\d+(?:[eE][+-]?\d+|)/.source,
rmargin = /^margin/, rnumnonpx = new RegExp( "^(" + core_pnum + ")(?!px)[a-z%]+$", "i" );
if(window.getComputedStyle){
getStyles = function(elem){return window.getComputedStyle( elem, null )};
curCSS = function( elem, name, _computed ){
var width, minWidth, maxWidth, computed = _computed || getStyles( elem ),
ret = computed ? computed.getPropertyValue( name ) || computed[ name ] : undefined,
style = elem.style;
if( computed ){
if( rnumnonpx.test( ret ) && rmargin.test( name )){
width = style.width; minWidth = style.minWidth; maxWidth = style.maxWidth;
style.minWidth = style.maxWidth = style.width = ret; ret = computed.width;
style.width = width; style.minWidth = minWidth; style.maxWidth = maxWidth}}
return ret;
}
}
else if (document.documentElement.currentStyle){
getStyles = function( elem ){return elem.currentStyle};
curCSS = function( elem, name, _computed ){
try{
var left, rs, rsLeft, computed = _computed || getStyles( elem ),
ret = computed ? computed[ name ] : undefined, style = elem.style;
if( ret == null && style && style[ name ] ) {ret = style[ name ]}
if( rnumnonpx.test( ret ) && !rposition.test( name ) ) {
left = style.left; rs = elem.runtimeStyle;rsLeft = rs && rs.left;
if ( rsLeft ) {rs.left = elem.currentStyle.left}
style.left = name === "fontSize" ? "1em" : ret; ret = style.pixelLeft + "px";
style.left = left; if ( rsLeft ) {rs.left = rsLeft}}
return ret === "" ? "auto" : ret
}
catch(e){};
}
}
})();