prop.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. if ( !support.optSelected ) {
  56. jQuery.propHooks.selected = {
  57. get: function( elem ) {
  58. var parent = elem.parentNode;
  59. if ( parent && parent.parentNode ) {
  60. parent.parentNode.selectedIndex;
  61. }
  62. return null;
  63. }
  64. };
  65. }
  66. jQuery.each([
  67. "tabIndex",
  68. "readOnly",
  69. "maxLength",
  70. "cellSpacing",
  71. "cellPadding",
  72. "rowSpan",
  73. "colSpan",
  74. "useMap",
  75. "frameBorder",
  76. "contentEditable"
  77. ], function() {
  78. jQuery.propFix[ this.toLowerCase() ] = this;
  79. });
  80. });