Interpretation of jQuery source code: removeClass () method analysis

Source: Internet
Author: User

Interpretation of jQuery source code: removeClass () method analysis

This article mainly introduces the removeClass () method for interpreting jQuery source code, and analyzes in detail the implementation skills and precautions of the removeClass () method in the form of annotations. For more information, see

 

 

This article analyzes in detail the removeClass () method for interpreting jQuery source code. Share it with you for your reference. The specific analysis is as follows:

The removeClass () method and addClass () method are slightly different. Let's take a look:

The Code is as follows:

JQuery. fn. extend ({
RemoveClass: function (value ){
Var classes, elem, cur, clazz, j, finalValue,
I = 0,
Len = this. length,
Proceed = arguments. length = 0 | typeof value = "string" & value;
If (jQuery. isFunction (value )){
Return this. each (function (j ){
// The removeClass itself is called again based on the class name returned by the function that you passed to remove the class name.
JQuery (this). removeClass (value. call (this, j, this. className ));
});
}
If (proceed ){
Classes = (value | ""). match (rnotwhite) | [];
For (; I <len; I ++ ){
Elem = this [I];
Cur = elem. nodeType === 1 & (elem. className?
("" + Elem. className + ""). replace (rclass ,""):
""
);
If (cur ){
J = 0;
While (clazz = classes [j ++]) {
// The difference lies in the while LOOP below. When the retrieved DOM element contains the name of the class you want to remove, replace it ""
While (cur. indexOf ("" + clazz + "")> = 0 ){
Cur = cur. replace ("" + clazz + "","");
}
}
// The following is also one of the key differences. Determine whether you have passed the class name value to be removed. If it is not passed, finalValue = "". If the DOM element has a class name, that is, if the condition is true, all class names of the DOM element are removed;
// If it is passed, the matched class name will be removed. After the class name is removed, the unremoved class names will be spliced into cur and the leading and trailing space strings will be removed, set the Class Name of the DOM element to cur.
FinalValue = value? JQuery. trim (cur ):"";
If (elem. className! = FinalValue ){
Elem. className = finalValue;
}
}
}
}
Return this;
}
});

 

I hope this article will help you with 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.