]> source.dussan.org Git - jquery.git/commitdiff
Avoid jQuery(this) and a closure for .toggle(Boolean), close gh-1271.
authorJason Bedard <github@jbedard.ca>
Sat, 11 May 2013 19:34:48 +0000 (12:34 -0700)
committerDave Methvin <dave.methvin@gmail.com>
Mon, 3 Jun 2013 02:35:49 +0000 (22:35 -0400)
(cherry picked from commit e53a91909061c7a7280a274990db179b94db81b6)

src/attributes.js
src/css.js

index 4ea1bd5cf62cf34c87f653bf8c42ac23736b8864..ce2a4f8094870c8296d9cd84b6a0012aab81a841 100644 (file)
@@ -111,8 +111,11 @@ jQuery.fn.extend({
        },
 
        toggleClass: function( value, stateVal ) {
-               var type = typeof value,
-                       isBool = typeof stateVal === "boolean";
+               var type = typeof value;
+
+               if ( typeof stateVal === "boolean" && type === "string" ) {
+                       return stateVal ? this.addClass( value ) : this.removeClass( value );
+               }
 
                if ( jQuery.isFunction( value ) ) {
                        return this.each(function( i ) {
@@ -126,13 +129,15 @@ jQuery.fn.extend({
                                var className,
                                        i = 0,
                                        self = jQuery( this ),
-                                       state = stateVal,
                                        classNames = value.match( core_rnotwhite ) || [];
 
                                while ( (className = classNames[ i++ ]) ) {
                                        // check each className given, space separated list
-                                       state = isBool ? state : !self.hasClass( className );
-                                       self[ state ? "addClass" : "removeClass" ]( className );
+                                       if ( self.hasClass( className ) ) {
+                                               self.removeClass( className );
+                                       } else {
+                                               self.addClass( className );
+                                       }
                                }
 
                        // Toggle whole class name
index d9bc15fc146ff0e3033f73173c97bec7518b4eba..12fc8e6e6be1a4b908a946d88f8acffc74f5ef70 100644 (file)
@@ -134,10 +134,12 @@ jQuery.fn.extend({
                return showHide( this );
        },
        toggle: function( state ) {
-               var bool = typeof state === "boolean";
+               if ( typeof state === "boolean" ) {
+                       return state ? this.show() : this.hide();
+               }
 
                return this.each(function() {
-                       if ( bool ? state : isHidden( this ) ) {
+                       if ( isHidden( this ) ) {
                                jQuery( this ).show();
                        } else {
                                jQuery( this ).hide();