mirror of
https://github.com/jquery/jquery.git
synced 2024-11-23 02:54:22 +00:00
Avoid jQuery(this) and a closure for .toggle(Boolean), close gh-1271.
(cherry picked from commit e53a919090
)
This commit is contained in:
parent
adf3090318
commit
9a3683b110
@ -111,8 +111,11 @@ jQuery.fn.extend({
|
|||||||
},
|
},
|
||||||
|
|
||||||
toggleClass: function( value, stateVal ) {
|
toggleClass: function( value, stateVal ) {
|
||||||
var type = typeof value,
|
var type = typeof value;
|
||||||
isBool = typeof stateVal === "boolean";
|
|
||||||
|
if ( typeof stateVal === "boolean" && type === "string" ) {
|
||||||
|
return stateVal ? this.addClass( value ) : this.removeClass( value );
|
||||||
|
}
|
||||||
|
|
||||||
if ( jQuery.isFunction( value ) ) {
|
if ( jQuery.isFunction( value ) ) {
|
||||||
return this.each(function( i ) {
|
return this.each(function( i ) {
|
||||||
@ -126,13 +129,15 @@ jQuery.fn.extend({
|
|||||||
var className,
|
var className,
|
||||||
i = 0,
|
i = 0,
|
||||||
self = jQuery( this ),
|
self = jQuery( this ),
|
||||||
state = stateVal,
|
|
||||||
classNames = value.match( core_rnotwhite ) || [];
|
classNames = value.match( core_rnotwhite ) || [];
|
||||||
|
|
||||||
while ( (className = classNames[ i++ ]) ) {
|
while ( (className = classNames[ i++ ]) ) {
|
||||||
// check each className given, space separated list
|
// check each className given, space separated list
|
||||||
state = isBool ? state : !self.hasClass( className );
|
if ( self.hasClass( className ) ) {
|
||||||
self[ state ? "addClass" : "removeClass" ]( className );
|
self.removeClass( className );
|
||||||
|
} else {
|
||||||
|
self.addClass( className );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Toggle whole class name
|
// Toggle whole class name
|
||||||
|
@ -134,10 +134,12 @@ jQuery.fn.extend({
|
|||||||
return showHide( this );
|
return showHide( this );
|
||||||
},
|
},
|
||||||
toggle: function( state ) {
|
toggle: function( state ) {
|
||||||
var bool = typeof state === "boolean";
|
if ( typeof state === "boolean" ) {
|
||||||
|
return state ? this.show() : this.hide();
|
||||||
|
}
|
||||||
|
|
||||||
return this.each(function() {
|
return this.each(function() {
|
||||||
if ( bool ? state : isHidden( this ) ) {
|
if ( isHidden( this ) ) {
|
||||||
jQuery( this ).show();
|
jQuery( this ).show();
|
||||||
} else {
|
} else {
|
||||||
jQuery( this ).hide();
|
jQuery( this ).hide();
|
||||||
|
Loading…
Reference in New Issue
Block a user