prop.js 2.7 KB

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