Interpreting jQuery source code with hasClass () method analysis _ jquery

Source: Internet
Author: User
This article mainly introduces the hasClass () method of jQuery source code interpretation, and analyzes the implementation skills of the hasClass () method in a more detailed manner in the form of annotations, which has some reference value, for more information about jQuery source code, see the hasClass () method. Share it with you for your reference. The specific analysis is as follows:

The 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.