diff --git a/src/attributes.js b/src/attributes.js index c4c75009..6aecf085 100644 --- a/src/attributes.js +++ b/src/attributes.js @@ -1,34 +1,6 @@ jQuery.fn.extend({ attr: function( name, value ) { - var elem, options, isFunction = jQuery.isFunction(value); - - if ( typeof name === "string" ) { // A single attribute - if ( value === undefined ) { // Query it on first element - return this.length ? - jQuery.attr( this[0], name ) : - null; - } else { // Set it on all elements - for ( var i = 0, l = this.length; i < l; i++ ) { - elem = this[i]; - if ( isFunction ) - value = value.call(elem,i); - jQuery.attr( elem, name, value ); - } - } - } else { // Multiple attributes to set on all - options = name; - for ( var i = 0, l = this.length; i < l; i++ ) { - elem = this[i]; - for ( name in options ) { - value = options[name]; - if ( jQuery.isFunction(value) ) - value = value.call(elem,i); - jQuery.attr( elem, name, value ); - } - } - } - - return this; + return access(this, name, value, true, jQuery.attr); }, addClass: function( value ) { diff --git a/src/core.js b/src/core.js index d2f82a6d..b1dde53a 100644 --- a/src/core.js +++ b/src/core.js @@ -553,6 +553,30 @@ function evalScript( i, elem ) { } } +function access( elems, key, value, exec, fn ) { + var l = elems.length; + + if ( typeof key === "object" ) { + for (var k in key) { + access(elems, k, key[k], exec, fn); + } + return elems; + } + + if (value !== undefined) { + exec = exec && jQuery.isFunction(value); + + for (var i = 0; i < l; i++) { + var elem = elems[i], + val = exec ? value.call(elem, i) : value; + fn(elem, key, val); + } + return elems; + } + + return l ? fn(elems[0], key) : null; +} + function now() { return (new Date).getTime(); } diff --git a/src/css.js b/src/css.js index 5dd8dd06..f47bc8e2 100644 --- a/src/css.js +++ b/src/css.js @@ -17,51 +17,17 @@ var rexclude = /z-?index|font-?weight|opacity|zoom|line-?height/i, }; jQuery.fn.css = function( name, value ) { - var options = name, isFunction = jQuery.isFunction( value ); - - if ( typeof name === "string" ) { - // Are we setting the style? - if ( value === undefined ) { - return this.length ? - jQuery.css( this[0], name ) : - null; - - // Convert name, value params to options hash format - } else { - options = {}; - options[ name ] = value; + return access( this, name, value, true, function( elem, name, value ) { + if (value === undefined) { + return jQuery.css( elem, name ); } - } - - var isFunction = {}; - - // For each value, determine whether it's a Function so we don't - // need to determine it again for each element - for ( var prop in options ) { - isFunction[prop] = jQuery.isFunction( options[prop] ); - } - - // For each element... - for ( var i = 0, l = this.length; i < l; i++ ) { - var elem = this[i]; - - // Set all the styles - for ( var prop in options ) { - value = options[prop]; - - if ( isFunction[prop] ) { - value = value.call( elem, i ); - } - - if ( typeof value === "number" && !rexclude.test(prop) ) { - value = value + "px"; - } - - jQuery.style( elem, prop, value ); + + if ( typeof value === "number" && !rexclude.test(name) ) { + value += "px"; } - } - return this; + jQuery.style( elem, name, value ); + }); }; jQuery.extend({ diff --git a/test/unit/attributes.js b/test/unit/attributes.js index e9880df2..9b5411f0 100644 --- a/test/unit/attributes.js +++ b/test/unit/attributes.js @@ -2,7 +2,10 @@ module("attributes"); test("attr(String)", function() { expect(27); + + // This one sometimes fails randomally ?! equals( jQuery('#text1').attr('value'), "Test", 'Check for value attribute' ); + equals( jQuery('#text1').attr('value', "Test2").attr('defaultValue'), "Test", 'Check for defaultValue attribute' ); equals( jQuery('#text1').attr('type'), "text", 'Check for type attribute' ); equals( jQuery('#radio1').attr('type'), "radio", 'Check for type attribute' );