Added test for toggleClass, fixed jQuery.className.has to accept both node and className (#762)

This commit is contained in:
Jörn Zaefferer 2007-01-08 16:12:20 +00:00
parent d97397d38e
commit 76d6d445bb
2 changed files with 15 additions and 2 deletions

View file

@ -429,6 +429,15 @@ test("removeClass(String) - add three classes and remove again", function() {
ok( pass, "Remove multiple classes" );
});
test("toggleClass(String)", function() {
var e = $("#firstp");
ok( !e.is(".test"), "Assert class not present" );
e.toggleClass("test");
ok( e.is(".test"), "Assert class present" );
e.toggleClass("test");
ok( !e.is(".test"), "Assert class not present" );
});
test("removeAttr(String", function() {
ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );
});

View file

@ -1227,20 +1227,24 @@ jQuery.extend({
},
className: {
// internal only, use addClass("class")
add: function( elem, c ){
jQuery.each( c.split(/\s+/), function(i, cur){
if ( !jQuery.className.has( elem.className, cur ) )
elem.className += ( elem.className ? " " : "" ) + cur;
});
},
// internal only, use removeClass("class")
remove: function( elem, c ){
elem.className = c ?
jQuery.grep( elem.className.split(/\s+/), function(cur){
return !jQuery.className.has( c, cur );
}).join(' ') : "";
},
has: function( classes, c ){
return classes && new RegExp("(^|\\s)" + c + "(\\s|$)").test( classes );
// internal only, use is(".class")
has: function( t, c ) {
t = t.className || t;
return t && new RegExp("(^|\\s)" + c + "(\\s|$)").test( t );
}
},