Hasclass () method Analysis of jquery source code interpretation _jquery

Source: Internet
Author: User

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.

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.