jQuery alterClass plugin: Remove element classes with wildcard matching. Optionally add classes.
/** | |
* jQuery alterClass plugin | |
* | |
* Remove element classes with wildcard matching. Optionally add classes: | |
* $( '#foo' ).alterClass( 'foo-* bar-*', 'foobar' ) | |
* | |
* Copyright (c) 2011 Pete Boere (the-echoplex.net) | |
* Free under terms of the MIT license: http://www.opensource.org/licenses/mit-license.php | |
* | |
*/ | |
(function ( $ ) { | |
$.fn.alterClass = function ( removals, additions ) { | |
var self = this; | |
if ( removals.indexOf( '*' ) === -1 ) { | |
// Use native jQuery methods if there is no wildcard matching | |
self.removeClass( removals ); | |
return !additions ? self : self.addClass( additions ); | |
} | |
var patt = new RegExp( '\\s' + | |
removals. | |
replace( /\*/g, '[A-Za-z0-9-_]+' ). | |
split( ' ' ). | |
join( '\\s|\\s' ) + | |
'\\s', 'g' ); | |
self.each( function ( i, it ) { | |
var cn = ' ' + it.className + ' '; | |
while ( patt.test( cn ) ) { | |
cn = cn.replace( patt, ' ' ); | |
} | |
it.className = $.trim( cn ); | |
}); | |
return !additions ? self : self.addClass( additions ); | |
}; | |
})( jQuery ); |
This comment has been minimized.
This comment has been minimized.
Good work! Very helpful... |
This comment has been minimized.
This comment has been minimized.
Thanks for the code, very helpful for me ! |
This comment has been minimized.
This comment has been minimized.
Great plugin it should be implement in jquery 1.12 or 2.2 ! |
This comment has been minimized.
This comment has been minimized.
Good work! |
This comment has been minimized.
This comment has been minimized.
Great function – thanks a lot. It also works with Zepto. Just pass in |
This comment has been minimized.
This comment has been minimized.
Great Work! Thanks a lot ;) |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
Very nice! Thanks a ton! |
This comment has been minimized.
This comment has been minimized.
Wonderful, thank you very much! |
This comment has been minimized.
This comment has been minimized.
Very useful, thanks |
This comment has been minimized.
This comment has been minimized.
Good work :) |
This comment has been minimized.
This comment has been minimized.
thanks a lot.. |
This comment has been minimized.
This comment has been minimized.
Nice. Very handy. +1 |
This comment has been minimized.
This comment has been minimized.
Nice ! thanks a lot |
This comment has been minimized.
This comment has been minimized.
Nice! Thanks +1 |
This comment has been minimized.
This comment has been minimized.
Saved my time... Thank you! |
This comment has been minimized.
This comment has been minimized.
正是我需要的 |
This comment has been minimized.
This comment has been minimized.
Saved a lot of my time, thanks |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
This is really useful! Is it possible to replace many classes pattern with one call? For example
'alert-* status-* type-*'
Currently this method only seems to work if the classes are in that order. Ideally, it would work regardless of the order.
Thoughts?