jquery core: Closes #5189. Added a generic function to handle getting/setting key-value/setting a hash.
This commit is contained in:
parent
aadc268abf
commit
d1285504fb
|
@ -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 ) {
|
||||
|
|
24
src/core.js
24
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();
|
||||
}
|
||||
|
|
50
src/css.js
50
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({
|
||||
|
|
|
@ -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' );
|
||||
|
|
Loading…
Reference in a new issue