首页 > 代码库 > jquery1.7.2的源码分析(六)基本功能

jquery1.7.2的源码分析(六)基本功能

jQuery.fn.extend({attr: function( name, value ) {return jQuery.access( this, jQuery.attr, name, value, arguments.length > 1 );},removeAttr: function( name ) {return this.each(function() {jQuery.removeAttr( this, name );});},prop: function( name, value ) {return jQuery.access( this, jQuery.prop, name, value, arguments.length > 1 );},removeProp: function( name ) {name = jQuery.propFix[ name ] || name;return this.each(function() {// try/catch handles cases where IE balks (such as removing a property on window)try {this[ name ] = undefined;delete this[ name ];} catch( e ) {}});},addClass: function( value ) {var classNames, i, l, elem,setClass, c, cl;console.log(this)if ( jQuery.isFunction( value ) ) {return this.each(function( j ) {jQuery( this ).addClass( value.call(this, j, this.className) );});}if ( value && typeof value =http://www.mamicode.com/=="string" ) {classNames = value.split( rspace );for ( i = 0, l = this.length; i < l; i++ ) {elem = this[ i ];if ( elem.nodeType === 1 ) {if ( !elem.className && classNames.length === 1 ) {elem.className = value;} else {setClass = " " + elem.className + " ";for ( c = 0, cl = classNames.length; c < cl; c++ ) {if ( !~setClass.indexOf( " " + classNames[ c ] + " " ) ) {setClass += classNames[ c ] + " ";}}elem.className = jQuery.trim( setClass );}}}}return this;},removeClass: function( value ) {var classNames, i, l, elem, className, c, cl;if ( jQuery.isFunction( value ) ) {return this.each(function( j ) {jQuery( this ).removeClass( value.call(this, j, this.className) );});}if ( (value && typeof value =http://www.mamicode.com/=="string") || value =http://www.mamicode.com/== undefined ) {"" ).split( rspace );for ( i = 0, l = this.length; i < l; i++ ) {elem = this[ i ];if ( elem.nodeType === 1 && elem.className ) {if ( value ) {className = (" " + elem.className + " ").replace( rclass, " " );for ( c = 0, cl = classNames.length; c < cl; c++ ) {className = className.replace(" " + classNames[ c ] + " ", " ");}elem.className = jQuery.trim( className );} else {elem.className = "";}}}}return this;},toggleClass: function( value, stateVal ) {var type = typeof value,isBool = typeof stateVal === "boolean";if ( jQuery.isFunction( value ) ) {return this.each(function( i ) {jQuery( this ).toggleClass( value.call(this, i, this.className, stateVal), stateVal );});}return this.each(function() {if ( type === "string" ) {// toggle individual class namesvar className,i = 0,self = jQuery( this ),state = stateVal,classNames = value.split( rspace );while ( (className = classNames[ i++ ]) ) {// check each className given, space seperated liststate = isBool ? state : !self.hasClass( className );self[ state ? "addClass" : "removeClass" ]( className );}} else if ( type === "undefined" || type === "boolean" ) {if ( this.className ) {// store className if setjQuery._data( this, "__className__", this.className );}// toggle whole classNamethis.className = this.className || value =http://www.mamicode.com/== false ?"" : jQuery._data( this, "__className__" ) || "";}});},hasClass: function( selector ) {var className = " " + selector + " ",i = 0,l = this.length;for ( ; i < l; i++ ) {if ( this[i].nodeType === 1 && (" " + this[i].className + " ").replace(rclass, " ").indexOf( className ) > -1 ) {return true;}}return false;},val: function( value ) {var hooks, ret, isFunction,elem = this[0];if ( !arguments.length ) {if ( elem ) {hooks = jQuery.valHooks[ elem.type ] || jQuery.valHooks[ elem.nodeName.toLowerCase() ];if ( hooks && "get" in hooks && (ret = hooks.get( elem, "value" )) !== undefined ) {return ret;}ret = elem.value;return typeof ret === "string" ?// handle most common string casesret.replace(rreturn, "") :// handle cases where value is null/undef or numberret == null ? "" : ret;}return;}isFunction = jQuery.isFunction( value );return this.each(function( i ) {var self = jQuery(this), val;if ( this.nodeType !== 1 ) {return;}if ( isFunction ) {val = value.call( this, i, self.val() );} else {val = value;}// Treat null/undefined as ""; convert numbers to stringif ( val == null ) {val = "";} else if ( typeof val === "number" ) {val += "";} else if ( jQuery.isArray( val ) ) {val = jQuery.map(val, function ( value ) {return value =http://www.mamicode.com/= null ?"" : value + "";});}hooks = jQuery.valHooks[ this.type ] || jQuery.valHooks[ this.nodeName.toLowerCase() ];// If set returns undefined, fall back to normal settingif ( !hooks || !("set" in hooks) || hooks.set( this, val, "value" ) === undefined ) {this.value = http://www.mamicode.com/val;"select-one";// Nothing was selectedif ( index < 0 ) {return null;}// Loop through all the selected optionsi = one ? index : 0;max = one ? index + 1 : options.length;for ( ; i < max; i++ ) {option = options[ i ];// Don‘t return options that are disabled or in a disabled optgroupif ( option.selected && (jQuery.support.optDisabled ? !option.disabled : option.getAttribute("disabled") === null) &&(!option.parentNode.disabled || !jQuery.nodeName( option.parentNode, "optgroup" )) ) {// Get the specific value for the optionvalue = http://www.mamicode.com/jQuery( option ).val();"option").each(function() {this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0;});if ( !values.length ) {elem.selectedIndex = -1;}return values;}}},attrFn: {val: true,css: true,html: true,text: true,data: true,width: true,height: true,offset: true},attr: function( elem, name, value, pass ) {var ret, hooks, notxml,nType = elem.nodeType;// don‘t get/set attributes on text, comment and attribute nodesif ( !elem || nType === 3 || nType === 8 || nType === 2 ) {return;}if ( pass && name in jQuery.attrFn ) {//相当于$(‘div‘).html(value)return jQuery( elem )[ name ]( value );}// Fallback to prop when attributes are not supported//当不支持getEttribute时,使用$.propif ( typeof elem.getAttribute === "undefined" ) {return jQuery.prop( elem, name, value );}//不是xml对象的时候notxml = nType !== 1 || !jQuery.isXMLDoc( elem );// All attributes are lowercase// Grab necessary hook if one is definedif ( notxml ) {name = name.toLowerCase();//假设传入的值为 $.attr("input"," value ",111 ,0)//hooks=$.attrHooks[‘value‘]hooks = jQuery.attrHooks[ name ] || ( rboolean.test( name ) ? boolHook : nodeHook );}if ( value !== undefined ) {if ( value =http://www.mamicode.com/== null ) {"set" in hooks && notxml && (ret = hooks.set( elem, value, name )) !== undefined ) {return ret;} else {elem.setAttribute( name, "" + value );return value;}} else if ( hooks && "get" in hooks && notxml && (ret = hooks.get( elem, name )) !== null ) {return ret;} else {ret = elem.getAttribute( name );// Non-existent attributes return null, we normalize to undefinedreturn ret === null ?undefined :ret;}},removeAttr: function( elem, value ) {var propName, attrNames, name, l, isBool,i = 0;if ( value && elem.nodeType === 1 ) {attrNames = value.toLowerCase().split( rspace );l = attrNames.length;for ( ; i < l; i++ ) {name = attrNames[ i ];if ( name ) {propName = jQuery.propFix[ name ] || name;isBool = rboolean.test( name );// See #9699 for explanation of this approach (setting first, then removal)// Do not do this for boolean attributes (see #10870)if ( !isBool ) {jQuery.attr( elem, name, "" );}elem.removeAttribute( getSetAttribute ? name : propName );// Set corresponding property to false for boolean attributesif ( isBool && propName in elem ) {elem[ propName ] = false;}}}}},attrHooks: {type: {set: function( elem, value ) {// We can‘t allow the type property to be changed (since it causes problems in IE)if ( rtype.test( elem.nodeName ) && elem.parentNode ) {jQuery.error( "type property can‘t be changed" );} else if ( !jQuery.support.radioValue && value =http://www.mamicode.com/=="radio" && jQuery.nodeName(elem, "input") ) {// Setting the type on a radio button after the value resets the value in IE6-9// Reset value to it‘s default in case type is set after value// This is for element creationvar val = elem.value;elem.setAttribute( "type", value );if ( val ) {elem.value = http://www.mamicode.com/val;"button" ) ) {return nodeHook.get( elem, name );}return name in elem ?elem.value :null;},set: function( elem, value, name ) {if ( nodeHook && jQuery.nodeName( elem, "button" ) ) {return nodeHook.set( elem, value, name );}// Does not return so that setAttribute is also usedelem.value = http://www.mamicode.com/value;"tabIndex",readonly: "readOnly","for": "htmlFor","class": "className",maxlength: "maxLength",cellspacing: "cellSpacing",cellpadding: "cellPadding",rowspan: "rowSpan",colspan: "colSpan",usemap: "useMap",frameborder: "frameBorder",contenteditable: "contentEditable"},prop: function( elem, name, value ) {var ret, hooks, notxml,nType = elem.nodeType;// don‘t get/set properties on text, comment and attribute nodesif ( !elem || nType === 3 || nType === 8 || nType === 2 ) {return;}notxml = nType !== 1 || !jQuery.isXMLDoc( elem );if ( notxml ) {// Fix name and attach hooksname = jQuery.propFix[ name ] || name;hooks = jQuery.propHooks[ name ];}if ( value !== undefined ) {if ( hooks && "set" in hooks && (ret = hooks.set( elem, value, name )) !== undefined ) {return ret;} else {return ( elem[ name ] = value );}} else {if ( hooks && "get" in hooks && (ret = hooks.get( elem, name )) !== null ) {return ret;} else {return elem[ name ];}}},propHooks: {tabIndex: {get: function( elem ) {// elem.tabIndex doesn‘t always return the correct value when it hasn‘t been explicitly set// http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/var attributeNode = elem.getAttributeNode("tabindex");return attributeNode && attributeNode.specified ?parseInt( attributeNode.value, 10 ) :rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?0 :undefined;}}}});// Add the tabIndex propHook to attrHooks for back-compat (different case is intentional)jQuery.attrHooks.tabindex = jQuery.propHooks.tabIndex;// Hook for boolean attributesboolHook = {get: function( elem, name ) {// Align boolean attributes with corresponding properties// Fall back to attribute presence where some booleans are not supportedvar attrNode,property = jQuery.prop( elem, name );return property === true || typeof property !== "boolean" && ( attrNode = elem.getAttributeNode(name) ) && attrNode.nodeValue !== false ?name.toLowerCase() :undefined;},set: function( elem, value, name ) {var propName;if ( value =http://www.mamicode.com/== false ) {>

jquery1.7.2的源码分析(六)基本功能