mirror of
https://github.com/jquery/jquery.git
synced 2026-04-20 03:01:22 -04:00
Attributes: allow array param in add/remove/toggleClass
+30 bytes instead of +182 Thanks to @faisaliyk for the first pass on this feature. Fixes gh-3532 Close gh-3917
This commit is contained in:
@@ -12,6 +12,16 @@ function getClass( elem ) {
|
||||
return elem.getAttribute && elem.getAttribute( "class" ) || "";
|
||||
}
|
||||
|
||||
function classesToArray( value ) {
|
||||
if ( Array.isArray( value ) ) {
|
||||
return value;
|
||||
}
|
||||
if ( typeof value === "string" ) {
|
||||
return value.match( rnothtmlwhite ) || [];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
jQuery.fn.extend( {
|
||||
addClass: function( value ) {
|
||||
var classes, elem, cur, curValue, clazz, j, finalValue,
|
||||
@@ -23,9 +33,9 @@ jQuery.fn.extend( {
|
||||
} );
|
||||
}
|
||||
|
||||
if ( typeof value === "string" && value ) {
|
||||
classes = value.match( rnothtmlwhite ) || [];
|
||||
classes = classesToArray( value );
|
||||
|
||||
if ( classes.length ) {
|
||||
while ( ( elem = this[ i++ ] ) ) {
|
||||
curValue = getClass( elem );
|
||||
cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " );
|
||||
@@ -64,9 +74,9 @@ jQuery.fn.extend( {
|
||||
return this.attr( "class", "" );
|
||||
}
|
||||
|
||||
if ( typeof value === "string" && value ) {
|
||||
classes = value.match( rnothtmlwhite ) || [];
|
||||
classes = classesToArray( value );
|
||||
|
||||
if ( classes.length ) {
|
||||
while ( ( elem = this[ i++ ] ) ) {
|
||||
curValue = getClass( elem );
|
||||
|
||||
@@ -96,9 +106,10 @@ jQuery.fn.extend( {
|
||||
},
|
||||
|
||||
toggleClass: function( value, stateVal ) {
|
||||
var type = typeof value;
|
||||
var type = typeof value,
|
||||
isValidValue = type === "string" || Array.isArray( value );
|
||||
|
||||
if ( typeof stateVal === "boolean" && type === "string" ) {
|
||||
if ( typeof stateVal === "boolean" && isValidValue ) {
|
||||
return stateVal ? this.addClass( value ) : this.removeClass( value );
|
||||
}
|
||||
|
||||
@@ -114,12 +125,12 @@ jQuery.fn.extend( {
|
||||
return this.each( function() {
|
||||
var className, i, self, classNames;
|
||||
|
||||
if ( type === "string" ) {
|
||||
if ( isValidValue ) {
|
||||
|
||||
// Toggle individual class names
|
||||
i = 0;
|
||||
self = jQuery( this );
|
||||
classNames = value.match( rnothtmlwhite ) || [];
|
||||
classNames = classesToArray( value );
|
||||
|
||||
while ( ( className = classNames[ i++ ] ) ) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user