Created
December 22, 2010 11:43
-
-
Save yoko/751419 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$.fn.replaceClass = function(proc) { | |
return this.each(function() { | |
var classes = this.className.split(/\s+/), new_classes = []; | |
for (var i = 0, klass, ret; klass = classes[i]; ++i) { | |
ret = proc.call(this, i, klass); | |
if (ret) { | |
new_classes.push(typeof ret == 'string' ? ret : classes[i]); | |
} | |
} | |
this.className = new_classes.join(' '); | |
}); | |
}; | |
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module('$.replaceClass'); | |
test('replaceClass', 3, function() { | |
var div; | |
div = $('<div class="foo bar baz"/>'); | |
div.replaceClass(function(i, klass) { | |
return klass == 'foo'; | |
}); | |
equal(div.attr('class'), 'foo'); | |
div = $('<div class="foo bar baz"/>'); | |
div.replaceClass(function(i, klass) { | |
return /a/.test(klass); | |
}); | |
equal(div.attr('class'), 'bar baz'); | |
div = $('<div class="foo bar baz"/>'); | |
div.replaceClass(function(i, klass) { | |
return klass+='2'; | |
}); | |
equal(div.attr('class'), 'foo2 bar2 baz2'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment