mirror of
https://github.com/jquery/jquery.git
synced 2025-01-10 18:24:24 +00:00
toggleClass can now toggle multiple classNames (space seperated list) and toggle the whole className. fixes #3825.
This commit is contained in:
parent
d415e0adb8
commit
5e6e53835e
@ -130,9 +130,25 @@ jQuery.each({
|
|||||||
},
|
},
|
||||||
|
|
||||||
toggleClass: function( classNames, state ) {
|
toggleClass: function( classNames, state ) {
|
||||||
if( typeof state !== "boolean" )
|
var type = typeof classNames;
|
||||||
state = !jQuery.className.has( this, classNames );
|
if ( type === "string" ) {
|
||||||
jQuery.className[ state ? "add" : "remove" ]( this, classNames );
|
// toggle individual class names
|
||||||
|
var isBool = typeof state === "boolean", className, i = 0,
|
||||||
|
classNames = classNames.split( /\s+/ );
|
||||||
|
while ( (className = classNames[ i++ ]) ) {
|
||||||
|
// check each className given, space seperated list
|
||||||
|
state = isBool ? state : !jQuery.className.has( this, className );
|
||||||
|
jQuery.className[ state ? "add" : "remove" ]( this, className );
|
||||||
|
}
|
||||||
|
} else if ( type === "undefined" || type === "boolean" ) {
|
||||||
|
// toggle whole className
|
||||||
|
if ( this.className || classNames === false ) {
|
||||||
|
jQuery.data( this, "__className__", this.className );
|
||||||
|
this.className = "";
|
||||||
|
} else {
|
||||||
|
this.className = jQuery.data( this, "__className__" ) || "";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, function(name, fn){
|
}, function(name, fn){
|
||||||
jQuery.fn[ name ] = function(){
|
jQuery.fn[ name ] = function(){
|
||||||
|
@ -281,8 +281,9 @@ test("removeClass(String) - simple", function() {
|
|||||||
ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
|
ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
|
||||||
});
|
});
|
||||||
|
|
||||||
test("toggleClass(String)", function() {
|
test("toggleClass(String|boolean|undefined[, boolean])", function() {
|
||||||
expect(6);
|
expect(16);
|
||||||
|
|
||||||
var e = jQuery("#firstp");
|
var e = jQuery("#firstp");
|
||||||
ok( !e.is(".test"), "Assert class not present" );
|
ok( !e.is(".test"), "Assert class not present" );
|
||||||
e.toggleClass("test");
|
e.toggleClass("test");
|
||||||
@ -290,12 +291,40 @@ test("toggleClass(String)", function() {
|
|||||||
e.toggleClass("test");
|
e.toggleClass("test");
|
||||||
ok( !e.is(".test"), "Assert class not present" );
|
ok( !e.is(".test"), "Assert class not present" );
|
||||||
|
|
||||||
|
// class name with a boolean
|
||||||
e.toggleClass("test", false);
|
e.toggleClass("test", false);
|
||||||
ok( !e.is(".test"), "Assert class not present" );
|
ok( !e.is(".test"), "Assert class not present" );
|
||||||
e.toggleClass("test", true);
|
e.toggleClass("test", true);
|
||||||
ok( e.is(".test"), "Assert class present" );
|
ok( e.is(".test"), "Assert class present" );
|
||||||
e.toggleClass("test", false);
|
e.toggleClass("test", false);
|
||||||
ok( !e.is(".test"), "Assert class not present" );
|
ok( !e.is(".test"), "Assert class not present" );
|
||||||
|
|
||||||
|
// multiple class names
|
||||||
|
e.addClass("testA testB");
|
||||||
|
ok( (e.is(".testA.testB")), "Assert 2 different classes present" );
|
||||||
|
e.toggleClass("testB testC");
|
||||||
|
ok( (e.is(".testA.testC") && !e.is(".testB")), "Assert 1 class added, 1 class removed, and 1 class kept" );
|
||||||
|
e.toggleClass("testA testC");
|
||||||
|
ok( (!e.is(".testA") && !e.is(".testB") && !e.is(".testC")), "Assert no class present" );
|
||||||
|
|
||||||
|
// toggleClass storage
|
||||||
|
e.toggleClass(true);
|
||||||
|
ok( e.get(0).className === "", "Assert class is empty (data was empty)" );
|
||||||
|
e.addClass("testD");
|
||||||
|
ok( e.is(".testD"), "Assert class present" );
|
||||||
|
e.toggleClass();
|
||||||
|
ok( !e.is(".testD"), "Assert class not present" );
|
||||||
|
ok( e.data('__className__') === 'testD', "Assert data was stored" );
|
||||||
|
e.toggleClass();
|
||||||
|
ok( e.is(".testD"), "Assert class present (restored from data)" );
|
||||||
|
e.toggleClass(false);
|
||||||
|
ok( !e.is(".testD"), "Assert class not present" );
|
||||||
|
e.toggleClass(true);
|
||||||
|
ok( e.is(".testD"), "Assert class present (restored from data)" );
|
||||||
|
|
||||||
|
// Cleanup
|
||||||
|
e.removeClass("testD");
|
||||||
|
e.removeData('__className__');
|
||||||
});
|
});
|
||||||
|
|
||||||
test("removeAttr(String", function() {
|
test("removeAttr(String", function() {
|
||||||
|
Loading…
Reference in New Issue
Block a user