From 65a88fae8919e5da774dcc45c9b42220f875b697 Mon Sep 17 00:00:00 2001 From: John Resig Date: Fri, 29 Dec 2006 18:04:26 +0000 Subject: [PATCH] Added support for adding multiple classes simultaneously, fixed a bug with remove class. --- src/jquery/jquery.js | 57 ++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index 88930f227..7b712c955 100644 --- a/src/jquery/jquery.js +++ b/src/jquery/jquery.js @@ -510,9 +510,9 @@ jQuery.fn = jQuery.prototype = { e = e || this; var t = ""; - for ( var j = 0; j < e.length; j++ ) { + for ( var j = 0, el = e.length; j < el; j++ ) { var r = e[j].childNodes; - for ( var i = 0; i < r.length; i++ ) + for ( var i = 0, rl = r.length; i < rl; i++ ) if ( r[i].nodeType != 8 ) t += r[i].nodeType != 1 ? r[i].nodeValue : jQuery.fn.text([ r[i] ]); @@ -882,7 +882,7 @@ jQuery.fn = jQuery.prototype = { return this.pushStack( t.constructor == Array && jQuery.map(this,function(a){ - for ( var i = 0; i < t.length; i++ ) + for ( var i = 0, tl = t.length; i < tl; i++ ) if ( jQuery.filter(t[i],[a]).r.length ) return a; return null; @@ -1030,7 +1030,7 @@ jQuery.fn = jQuery.prototype = { if ( table && this.nodeName.toUpperCase() == "TABLE" && a[0].nodeName.toUpperCase() == "TR" ) obj = this.getElementsByTagName("tbody")[0] || this.appendChild(document.createElement("tbody")); - for ( var i=0; i < a.length; i++ ) + for ( var i = 0, al = a.length; i < al; i++ ) fn.apply( obj, [ clone ? a[i].cloneNode(true) : a[i] ] ); }); @@ -1164,7 +1164,7 @@ jQuery.extend({ jQuery.fn[ i ] = function(){ var a = arguments; return this.each(function(){ - for ( var j = 0; j < a.length; j++ ) + for ( var j = 0, al = a.length; j < al; j++ ) jQuery(a[j])[n]( this ); }); }; @@ -1229,27 +1229,26 @@ jQuery.extend({ for ( var i in obj ) fn.apply( obj[i], args || [i, obj[i]] ); else - for ( var i = 0; i < obj.length; i++ ) + for ( var i = 0, ol = obj.length; i < ol; i++ ) if ( fn.apply( obj[i], args || [i, obj[i]] ) === false ) break; return obj; }, className: { add: function( elem, c ){ - if ( jQuery.className.has( elem, c ) ) - return; - elem.className += ( elem.className ? " " : "" ) + c; + jQuery.each( c.split(/\s+/), function(i, cur){ + if ( !jQuery.className.has( elem.className, cur ) ) + elem.className += ( elem.className ? " " : "" ) + cur; + }); }, remove: function( elem, c ){ elem.className = c ? - jQuery.grep( elem.className.split(/\s+/), function(cur){ - return jQuery.className.has( c, cur ); + jQuery.grep( elem.className.split(/\s+/), function(cur){ + return !jQuery.className.has( c, cur ); }).join(' ') : ""; }, - has: function( elem, c ){ - if ( elem.className != undefined ) - elem = elem.className; - return new RegExp("(^|\\s)" + c + "(\\s|$)").test( elem ); + has: function( classes, c ){ + return classes && new RegExp("(^|\\s)" + c + "(\\s|$)").test( classes ); } }, @@ -1271,7 +1270,7 @@ jQuery.extend({ if ( p == "height" || p == "width" ) { var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"]; - for ( var i=0; i