prop.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. define([
  2. "../core",
  3. "../core/access",
  4. "./support"
  5. ], function( jQuery, access, support ) {
  6. var rfocusable = /^(?:input|select|textarea|button)$/i;
  7. jQuery.fn.extend({
  8. prop: function( name, value ) {
  9. return access( this, jQuery.prop, name, value, arguments.length > 1 );
  10. },
  11. removeProp: function( name ) {
  12. return this.each(function() {
  13. delete this[ jQuery.propFix[ name ] || name ];
  14. });
  15. }
  16. });
  17. jQuery.extend({
  18. propFix: {
  19. "for": "htmlFor",
  20. "class": "className"
  21. },
  22. prop: function( elem, name, value ) {
  23. var ret, hooks, notxml,
  24. nType = elem.nodeType;
  25. // don't get/set properties on text, comment and attribute nodes
  26. if ( !elem || nType === 3 || nType === 8 || nType === 2 ) {
  27. return;
  28. }
  29. notxml = nType !== 1 || !jQuery.isXMLDoc( elem );
  30. if ( notxml ) {
  31. // Fix name and attach hooks
  32. name = jQuery.propFix[ name ] || name;
  33. hooks = jQuery.propHooks[ name ];
  34. }
  35. if ( value !== undefined ) {
  36. return hooks && "set" in hooks && (ret = hooks.set( elem, value, name )) !== undefined ?
  37. ret :
  38. ( elem[ name ] = value );
  39. } else {
  40. return hooks && "get" in hooks && (ret = hooks.get( elem, name )) !== null ?
  41. ret :
  42. elem[ name ];
  43. }
  44. },
  45. propHooks: {
  46. tabIndex: {
  47. get: function( elem ) {
  48. return elem.hasAttribute( "tabindex" ) || rfocusable.test( elem.nodeName ) || elem.href ?
  49. elem.tabIndex :
  50. -1;
  51. }
  52. }
  53. }
  54. });
  55. // Support: IE9+
  56. // Selectedness for an option in an optgroup can be inaccurate
  57. if ( !support.optSelected ) {
  58. jQuery.propHooks.selected = {
  59. get: function( elem ) {
  60. var parent = elem.parentNode;
  61. if ( parent && parent.parentNode ) {
  62. parent.parentNode.selectedIndex;
  63. }
  64. return null;
  65. }
  66. };
  67. }
  68. jQuery.each([
  69. "tabIndex",
  70. "readOnly",
  71. "maxLength",
  72. "cellSpacing",
  73. "cellPadding",
  74. "rowSpan",
  75. "colSpan",
  76. "useMap",
  77. "frameBorder",
  78. "contentEditable"
  79. ], function() {
  80. jQuery.propFix[ this.toLowerCase() ] = this;
  81. });
  82. });