prop.js (2857B)
1 define( [ 2 "../core", 3 "../core/access", 4 "./support", 5 "../selector" 6 ], function( jQuery, access, support ) { 7 8 "use strict"; 9 10 var rfocusable = /^(?:input|select|textarea|button)$/i, 11 rclickable = /^(?:a|area)$/i; 12 13 jQuery.fn.extend( { 14 prop: function( name, value ) { 15 return access( this, jQuery.prop, name, value, arguments.length > 1 ); 16 }, 17 18 removeProp: function( name ) { 19 return this.each( function() { 20 delete this[ jQuery.propFix[ name ] || name ]; 21 } ); 22 } 23 } ); 24 25 jQuery.extend( { 26 prop: function( elem, name, value ) { 27 var ret, hooks, 28 nType = elem.nodeType; 29 30 // Don't get/set properties on text, comment and attribute nodes 31 if ( nType === 3 || nType === 8 || nType === 2 ) { 32 return; 33 } 34 35 if ( nType !== 1 || !jQuery.isXMLDoc( elem ) ) { 36 37 // Fix name and attach hooks 38 name = jQuery.propFix[ name ] || name; 39 hooks = jQuery.propHooks[ name ]; 40 } 41 42 if ( value !== undefined ) { 43 if ( hooks && "set" in hooks && 44 ( ret = hooks.set( elem, value, name ) ) !== undefined ) { 45 return ret; 46 } 47 48 return ( elem[ name ] = value ); 49 } 50 51 if ( hooks && "get" in hooks && ( ret = hooks.get( elem, name ) ) !== null ) { 52 return ret; 53 } 54 55 return elem[ name ]; 56 }, 57 58 propHooks: { 59 tabIndex: { 60 get: function( elem ) { 61 62 // Support: IE <=9 - 11 only 63 // elem.tabIndex doesn't always return the 64 // correct value when it hasn't been explicitly set 65 // Use proper attribute retrieval (trac-12072) 66 var tabindex = jQuery.find.attr( elem, "tabindex" ); 67 68 if ( tabindex ) { 69 return parseInt( tabindex, 10 ); 70 } 71 72 if ( 73 rfocusable.test( elem.nodeName ) || 74 rclickable.test( elem.nodeName ) && 75 elem.href 76 ) { 77 return 0; 78 } 79 80 return -1; 81 } 82 } 83 }, 84 85 propFix: { 86 "for": "htmlFor", 87 "class": "className" 88 } 89 } ); 90 91 // Support: IE <=11 only 92 // Accessing the selectedIndex property 93 // forces the browser to respect setting selected 94 // on the option 95 // The getter ensures a default option is selected 96 // when in an optgroup 97 // eslint rule "no-unused-expressions" is disabled for this code 98 // since it considers such accessions noop 99 if ( !support.optSelected ) { 100 jQuery.propHooks.selected = { 101 get: function( elem ) { 102 103 /* eslint no-unused-expressions: "off" */ 104 105 var parent = elem.parentNode; 106 if ( parent && parent.parentNode ) { 107 parent.parentNode.selectedIndex; 108 } 109 return null; 110 }, 111 set: function( elem ) { 112 113 /* eslint no-unused-expressions: "off" */ 114 115 var parent = elem.parentNode; 116 if ( parent ) { 117 parent.selectedIndex; 118 119 if ( parent.parentNode ) { 120 parent.parentNode.selectedIndex; 121 } 122 } 123 } 124 }; 125 } 126 127 jQuery.each( [ 128 "tabIndex", 129 "readOnly", 130 "maxLength", 131 "cellSpacing", 132 "cellPadding", 133 "rowSpan", 134 "colSpan", 135 "useMap", 136 "frameBorder", 137 "contentEditable" 138 ], function() { 139 jQuery.propFix[ this.toLowerCase() ] = this; 140 } ); 141 142 } );