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.