jQuery hasclass structure
See t开发者_运维技巧his link: http://james.padolsey.com/jquery/#v=1.4&fn=jQuery.fn.hasClass
var className = " " + selector + " ";
....
(" " + this[i].className + " ")
Many times in my career I've had uses for functions that trim whitespace, I have never seen a reason to add whitespace. What is the purpose of the empty strings being prepended and appended to these variables.
Classes in HTML are space-separated in the markup. Rather than performing a string split, followed by searching through the array, the spaces are added so that even if that class is the only class applied to that element, it will still be matched by the selector.
Say you have an element with the classes foo
and fooBar
, adding the extra spaces at the start and end, ensures you always have spaces sourrding the name of each class.
This allows you to search for " foo "
and avoid matching fooBar
.
Off: I moved this to an answer so that the question could be marked as answered.
精彩评论