function hasClass( elem, cls ) {
var reClass = new RegExp(' ' + cls + ' ');
return reClass.test(' ' + elem.className + ' ');
}
i am a new learner of javascript, i saw the above function from a book.but i don’t what’s the meaning of it? expect someone can explain it to me.many thanks.
It’s just testing whether a given element contains a given class. This is useful for elements with multiple classes (older browsers don’t support that by default).
Consider an element like
<div id='test' class="button login"></div>. If you pass that to the function, you’ll get as follows:Demo here