prop.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. define( [
  2. "../core",
  3. "../core/access",
  4. "./support",
  5. "../selector"
  6. ], function( jQuery, access, support ) {
  7. var rfocusable = /^(?:input|select|textarea|button)$/i,
  8. rclickable = /^(?:a|area)$/i;
  9. jQuery.fn.extend( {
  10. prop: function( name, value ) {
  11. return access( this, jQuery.prop, name, value, arguments.length > 1 );
  12. },
  13. removeProp: function( name ) {
  14. return this.each( function() {
  15. delete this[ jQuery.propFix[ name ] || name ];
  16. } );
  17. }
  18. } );
  19. jQuery.extend( {
  20. prop: function( elem, name, value ) {
  21. var ret, hooks,
  22. nType = elem.nodeType;
  23. // Don't get/set properties on text, comment and attribute nodes
  24. if ( nType === 3 || nType === 8 || nType === 2 ) {
  25. return;
  26. }
  27. if ( nType !== 1 || !jQuery.isXMLDoc( elem ) ) {
  28. // Fix name and attach hooks
  29. name = jQuery.propFix[ name ] || name;
  30. hooks = jQuery.propHooks[ name ];
  31. }
  32. if ( value !== undefined ) {
  33. if ( hooks && "set" in hooks &&
  34. ( ret = hooks.set( elem, value, name ) ) !== undefined ) {
  35. return ret;
  36. }
  37. return ( elem[ name ] = value );
  38. }
  39. if ( hooks && "get" in hooks && ( ret = hooks.get( elem, name ) ) !== null ) {
  40. return ret;
  41. }
  42. return elem[ name ];
  43. },
  44. propHooks: {
  45. tabIndex: {
  46. get: function( elem ) {
  47. // elem.tabIndex doesn't always return the
  48. // correct value when it hasn't been explicitly set
  49. // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
  50. // Use proper attribute retrieval(#12072)
  51. var tabindex = jQuery.find.attr( elem, "tabindex" );
  52. return tabindex ?
  53. parseInt( tabindex, 10 ) :
  54. rfocusable.test( elem.nodeName ) ||
  55. rclickable.test( elem.nodeName ) && elem.href ?
  56. 0 :
  57. -1;
  58. }
  59. }
  60. },
  61. propFix: {
  62. "for": "htmlFor",
  63. "class": "className"
  64. }
  65. } );
  66. // Support: IE <=11 only
  67. // Accessing the selectedIndex property
  68. // forces the browser to respect setting selected
  69. // on the option
  70. // The getter ensures a default option is selected
  71. // when in an optgroup
  72. if ( !support.optSelected ) {
  73. jQuery.propHooks.selected = {
  74. get: function( elem ) {
  75. var parent = elem.parentNode;
  76. if ( parent && parent.parentNode ) {
  77. parent.parentNode.selectedIndex;
  78. }
  79. return null;
  80. },
  81. set: function( elem ) {
  82. var parent = elem.parentNode;
  83. if ( parent ) {
  84. parent.selectedIndex;
  85. if ( parent.parentNode ) {
  86. parent.parentNode.selectedIndex;
  87. }
  88. }
  89. }
  90. };
  91. }
  92. jQuery.each( [
  93. "tabIndex",
  94. "readOnly",
  95. "maxLength",
  96. "cellSpacing",
  97. "cellPadding",
  98. "rowSpan",
  99. "colSpan",
  100. "useMap",
  101. "frameBorder",
  102. "contentEditable"
  103. ], function() {
  104. jQuery.propFix[ this.toLowerCase() ] = this;
  105. } );
  106. } );