Interpretation of jQuery source code with hasClass () method analysis, jqueryhasclass
This article analyzes in detail the hasClass () method of jQuery source code interpretation. Share it with you for your reference. The specific analysis is as follows:
Copy codeThe Code is as follows: jQuery. fn. extend ({
HasClass: function (selector ){
// The selector of the class to be checked is assigned to className, and l is the length of the array of jQuery objects to be checked currently selected by the selector.
Var className = "" + selector + "",
I = 0,
L = this. length;
// Check the class name of each DOM element cyclically
For (; I <l; I ++ ){
// This [I]. nodeType = 1. Judge the node type of the current DOM node. 1 indicates the element node.
// This [I]. className to obtain the existing class name of the current DOM node.
// Rclass =/[\ t \ r \ n \ f]/g, replace (rclass, "") indicates removing the tabs in the Class Name of the current DOM node, line break, carriage return.
// IndexOf (className), starts to search for the class name you want to check in the Class Name of the current DOM node. If it is greater than or equal to 0, it indicates existence, returns true, and jumps out of the function.
If (this [I]. nodeType = 1 & ("" + this [I]. className + ""). replace (rclass ,""). indexOf (className)> = 0 ){
Return true;
}
}
// After the cyclic check is complete, if the class name you want to check is not found in every DOM element, false is returned and the function jumps out.
// Visible, as long as your jQuery object array finds that the class name of a DOM element contains the class name you want to search for, return true and jump out of the function.
Return false;
}
});
I hope this article will help you with jQuery programming.