This paper analyzes the Hasclass () method of jquery source interpretation in detail. Share to everyone for your reference. The specific analysis is as follows:
Copy Code code as follows:
JQuery.fn.extend ({
Hasclass:function (selector) {
The class name selector to be checked is assigned to classname, l the length of the jquery object array selected for the selector that is currently being checked.
var className = "" + selector + "",
i = 0,
L = this.length;
Iterate through the class name of each DOM element
for (; I < L; i++) {
This[i].nodetype = = 1, which determines the node type of the current DOM node, and 1 represents the element node.
This[i].classname, gets the class name that the current DOM node already exists.
Rclass =/[\t\r\n\f]/g,replace (Rclass, "") represents the removal of tabs, line breaks, carriage returns, and so on in the name of the current DOM node class.
IndexOf (ClassName), start retrieving the class name ClassName of the current DOM node, if >=0, 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 loop check, I found that every DOM element did not find the name of the class you want to check, then return false and jump out of the function.
See, as long as you have a jquery object array that finds a DOM element whose class name contains the name of the class you are looking for, return true and jump out of the function.
return false;
}
});
I hope this article will help you with your jquery programming.