X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fcss.js;h=99cb73581cd4430aeeb8bd2696b364514e0f849d;hb=2ca36598954759c5b5dce569a39c52b981ed4ab2;hp=84b6e6023f2321c01763bd84ca5d0bac1be41ccd;hpb=77e310b906112c9e20dfbf0e91249a0b81cf6107;p=jquery.git diff --git a/src/css.js b/src/css.js index 84b6e60..99cb735 100644 --- a/src/css.js +++ b/src/css.js @@ -1,6 +1,6 @@ (function( jQuery ) { -var ralpha = /alpha\([^)]*\)/, +var ralpha = /alpha\([^)]*\)/i, ropacity = /opacity=([^)]*)/, rdashAlpha = /-([a-z])/ig, rupper = /([A-Z])/g, @@ -20,6 +20,11 @@ var ralpha = /alpha\([^)]*\)/, }; jQuery.fn.css = function( name, value ) { + // Setting 'undefined' is a no-op + if ( arguments.length === 2 && value === undefined ) { + return this; + } + return jQuery.access( this, name, value, true, function( elem, name, value ) { return value !== undefined ? jQuery.style( elem, name, value ) : @@ -32,10 +37,15 @@ jQuery.extend({ // behavior of getting and setting a style property cssHooks: { opacity: { - get: function( elem ) { - // We should always get a number back from opacity - var ret = curCSS( elem, "opacity", "opacity" ); - return ret === "" ? "1" : ret; + get: function( elem, computed ) { + if ( computed ) { + // We should always get a number back from opacity + var ret = curCSS( elem, "opacity", "opacity" ); + return ret === "" ? "1" : ret; + + } else { + return elem.style.opacity; + } } } }, @@ -78,7 +88,11 @@ jQuery.extend({ // If a hook was provided, use that value, otherwise just set the specified value if ( !hooks || !("set" in hooks) || (value = hooks.set( elem, value )) !== undefined ) { - style[ name ] = value; + // Wrapped to prevent IE from throwing errors when 'invalid' values are provided + // Fixes bug #5509 + try { + style[ name ] = value; + } catch(e) {} } } else { @@ -155,7 +169,7 @@ jQuery.each(["height", "width"], function( i, name ) { }, set: function( elem, value ) { - if ( value !== "" ) { + if ( rnumpx.test( value ) ) { // ignore negative width and height values #1599 value = parseFloat(value); @@ -174,9 +188,9 @@ if ( !jQuery.support.opacity ) { jQuery.cssHooks.opacity = { get: function( elem, computed ) { // IE uses filters for opacity - return ropacity.test((computed ? elem.currentStyle.filter : elem.style.filter) || "") ? + return ropacity.test((computed && elem.currentStyle ? elem.currentStyle.filter : elem.style.filter) || "") ? (parseFloat(RegExp.$1) / 100) + "" : - "1"; + computed ? "1" : ""; }, set: function( elem, value ) { @@ -187,15 +201,14 @@ if ( !jQuery.support.opacity ) { style.zoom = 1; // Set the alpha filter to set the opacity - var opacity = parseInt( value, 10 ) + "" === "NaN" ? + var opacity = jQuery.isNaN(value) ? "" : - "alpha(opacity=" + value * 100 + ")"; - - var filter = style.filter || elem.currentStyle.filter || ""; + "alpha(opacity=" + value * 100 + ")", + filter = style.filter || ""; style.filter = ralpha.test(filter) ? filter.replace(ralpha, opacity) : - opacity; + style.filter + ' ' + opacity; } }; } @@ -219,7 +232,7 @@ if ( getComputedStyle ) { } else if ( document.documentElement.currentStyle ) { curCSS = function( elem, name ) { - var left, rsLeft, ret = elem.currentStyle[ name ], style = elem.style; + var left, rsLeft, ret = elem.currentStyle && elem.currentStyle[ name ], style = elem.style; // From the awesome hack by Dean Edwards // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291