addClass(className):为匹配的元素添加类名
@param (string) className
@return (jQuery)
1 /* 2 1.对传进的参数value进行检测 3 */ 4 if ( value && typeof value === "string" ) { 5 /* 6 2.将参数value分割为数组classNames 7 */ 8 classNames = value.split( rspace ); 9 /* 10 3.对dom数组进行循环遍历 11 */ 12 for ( i = 0, l = this.length; i < l; i++ ) { 13 elem = this[ i ]; 14 /* 15 4.判断当前dom的节点是否为ELEMENT类型 16 */ 17 if ( elem.nodeType === 1 ) { 18 /* 19 5.如果classNames长度只有一个,直接进行赋值结束 20 */ 21 if ( !elem.className && classNames.length === 1 ) { 22 elem.className = value; 23 24 } else { 25 /* 26 6.保存dom原来的class值 27 */ 28 setClass = " " + elem.className + " "; 29 /* 30 7.对classNames进行遍历,如果发现原class值没有包含,则添加进来 31 */ 32 for ( c = 0, cl = classNames.length; c < cl; c++ ) { 33 if ( !~setClass.indexOf( " " + classNames[ c ] + " " ) ) { 34 setClass += classNames[ c ] + " "; 35 } 36 } 37 elem.className = jQuery.trim( setClass ); 38 } 39 } 40 } 41 } 42 43 return this;