Home  >  Article  >  Web Front-end  >  jQuery source code interpretation hasClass() method analysis_jquery

jQuery source code interpretation hasClass() method analysis_jquery

WBOY
WBOYOriginal
2016-05-16 16:13:301543browse

This article analyzes the hasClass() method of jQuery source code interpretation in more detail. Share it with everyone for your reference. The specific analysis is as follows:

Copy code The code is as follows:
jQuery.fn.extend({
HasClass: function( selector ) {
//Assign the class name selector to be checked to className, and l is the length of the jQuery object array currently to be checked selected by the selector.
          var className = " " selector " ",
            i = 0,
              l = this.length;
//Loop to check the class name of each DOM element
for ( ; i < l; i ) {
//this[i].nodeType === 1, determine the node type of the current DOM node, 1 represents the element node.
//this[i].className, get the existing class name of the current DOM node.
//rclass = /[trnf]/g, replace(rclass, " ") means to remove tab characters, line feed characters, carriage return characters, etc. in the current DOM node class name.
//indexOf(className), start to search the class name of the current DOM node to see if there is the class name you want to check. If >=0, it means it exists, return true, and jump out of the function.
If ( this[i].nodeType === 1 && (" " this[i].className " ").replace(rclass, " ").indexOf( className ) >= 0 ) {
                   return true;
            }
}
//After the loop check is completed, it is found that the class name you want to check is not found in each DOM element, then return false and jump out of the function.
//It can be seen that as long as the class name of a DOM element in your jQuery object array contains the class name you are looking for, it will return true and jump out of the function.
         return false;
}
});

I hope this article will be helpful to everyone’s jQuery programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn