jQuery¼¼Êõ

jQuery ÖÐ attr() ºÍ prop() ·½·¨µÄÇø±ð

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2017-06-09 10:01 ÎÒÒªÆÀÂÛ( )

jQuery ÖÐ attr() ºÍ prop() ·½·¨µÄÇø±ð ·ÖÀࣺǰ¶Ë¼¼Êõ | ±êÇ©£ºjQuery¡¢attr¡¢prop | ·¢²¼Ê±¼ä£º2013-05-24 01:11:00 Ç°¼¸Ì죬ÓÐÈ˸ø Multiple Select ²å¼þ ÌáÁËÎÊÌ⣺ setSelects doesnt work in Firefox when using jquery 1.9.0 Ò»Ö±¶¼ÔÚÓà jQuery 1.8.

jQuery ÖÐ attr() ºÍ prop() ·½·¨µÄÇø±ð

·ÖÀࣺǰ¶Ë¼¼Êõ | ±êÇ©£ºjQuery¡¢attr¡¢prop | ·¢²¼Ê±¼ä£º2013-05-24 01:11:00

Ç°¼¸Ì죬ÓÐÈ˸ø Multiple Select ²å¼þ ÌáÁËÎÊÌ⣺

setSelects doesn't work in Firefox when using jquery 1.9.0

Ò»Ö±¶¼ÔÚÓà jQuery 1.8.3 µÄ°æ±¾£¬Ã»Óг¢ÊÔ¹ý jQuery 1.9.0 µÄ°æ±¾¡£

ÓÚÊÇ£¬¿ªÊ¼µ÷ÊÔ´úÂ룬ÔÚ 1.9.0 µÄ°æ±¾ÖУº

<input type="checkbox" /> <script> $(function() { $('input').click(function() { $(this).attr('checked'); }); }); </script>

µã»÷ checkbox£¬½á¹û¶¼ÊÇ undefined

¶øÔÚ 1.8.3 µÄ°æ±¾ÖУ¬½á¹ûÊÇ checked ºÍ undefined

µ½ÕâÀÎÊÌâ´ð°¸ÕÒµ½ÁË£¬¾ÍÊÇʹÓà attr() ·½·¨µÄÎÊÌ⣬ÓÚÊDz鿴¹Ù·½Îĵµ£¬ ²ÅÖªµÀ´Ó jQuery 1.6 ¿ªÊ¼ÐÂÔöÁËÒ»¸ö·½·¨ prop()£¬µ«ÊÇÒ»Ö±¶¼Ã»ÓÐʹÓùý¡£

´ÓÖÐÎÄÒâ˼¿´£¬Á½Õß·Ö±ðÊÇ»ñÈ¡/ÉèÖà attributes ºÍ properties µÄ·½·¨£¬ÄÇôΪʲô»¹ÒªÔö¼Ó prop() ·½·¨ÄØ£¿

Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior.

ÒòΪÔÚ jQuery 1.6 ֮ǰ£¬Ê¹Óà attr() ÓÐʱºò»á³öÏÖ²»Ò»ÖµÄÐÐΪ¡£

ÄÇô£¬Ê²Ã´Ê±ºòʹÓÃattr()£¬Ê²Ã´Ê±ºòʹÓÃprop()£¿

To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method.

¸ù¾Ý¹Ù·½µÄ½¨Ò飺¾ßÓÐ true ºÍ false Á½¸öÊôÐÔµÄÊôÐÔ£¬Èç checked, selected »òÕß disabled ʹÓÃprop()£¬ÆäËûµÄʹÓà attr()

µ½´Ë£¬½« attr('checked') ¸Ä³É prop('checked') ¼´¿ÉÐÞ¸´ÌáµÄ issues ÁË¡£

^_^

µÈµÈ£¬Ã²ËÆÎÊÌ⻹ûÕæÕý½â¾ö£¬ÎªÊ²Ã´¿ªÍ·Àý×ÓÖÐ jQuery 1.8.3 ºÍ 1.9.0 ʹÓà attr() »áÓÐËùÇø±ðÄØ£¿

ÏëÖªµÀËûÃǵÄÇø±ð£¬×îºÃµÄ°ì·¨»¹ÊÇ¿´ËûÃǵÄÔ´´úÂ룺

1.8.3 attr()£º

attr: function( elem, name, value, pass ) { var ret, hooks, notxml, nType = elem.nodeType; // don't get/set attributes on text, comment and attribute nodes if ( !elem || nType === 3 || nType === 8 || nType === 2 ) { return; } if ( pass && jQuery.isFunction( jQuery.fn[ name ] ) ) { return jQuery( elem )[ name ]( value ); } // Fallback to prop when attributes are not supported if ( typeof elem.getAttribute === "undefined" ) { return jQuery.prop( elem, name, value ); } notxml = nType !== 1 || !jQuery.isXMLDoc( elem ); // All attributes are lowercase // Grab necessary hook if one is defined if ( notxml ) { name = name.toLowerCase(); hooks = jQuery.attrHooks[ name ] || ( rboolean.test( name ) ? boolHook : nodeHook ); } if ( value !== undefined ) { if ( value === null ) { jQuery.removeAttr( elem, name ); return; } else if ( hooks && "set" in hooks && notxml && (ret = hooks.set( elem, value, name )) !== undefined ) { return ret; } else { elem.setAttribute( name, value + "" ); return value; } } else if ( hooks && "get" in hooks && notxml && (ret = hooks.get( elem, name )) !== null ) { return ret; } else { ret = elem.getAttribute( name ); // Non-existent attributes return null, we normalize to undefined return ret === null ? undefined : ret; } }

1.9.0 attr()£º

attr: function( elem, name, value ) { var ret, hooks, notxml, nType = elem.nodeType; // don't get/set attributes on text, comment and attribute nodes if ( !elem || nType === 3 || nType === 8 || nType === 2 ) { return; } // Fallback to prop when attributes are not supported if ( typeof elem.getAttribute === "undefined" ) { return jQuery.prop( elem, name, value ); } notxml = nType !== 1 || !jQuery.isXMLDoc( elem ); // All attributes are lowercase // Grab necessary hook if one is defined if ( notxml ) { name = name.toLowerCase(); hooks = jQuery.attrHooks[ name ] || ( rboolean.test( name ) ? boolHook : nodeHook ); } if ( value !== undefined ) { if ( value === null ) { jQuery.removeAttr( elem, name ); } else if ( hooks && notxml && "set" in hooks && (ret = hooks.set( elem, value, name )) !== undefined ) { return ret; } else { elem.setAttribute( name, value + "" ); return value; } } else if ( hooks && notxml && "get" in hooks && (ret = hooks.get( elem, name )) !== null ) { return ret; } else { // In IE9+, Flash objects don't have .getAttribute (#12945) // Support: IE9+ if ( typeof elem.getAttribute !== "undefined" ) { ret = elem.getAttribute( name ); } // Non-existent attributes return null, we normalize to undefined return ret == null ? undefined : ret; } }

1.8.3 ºÍ 1.9.0 µÄ prop() ÊÇÒ»ÑùµÄ:

prop: function( elem, name, value ) { var ret, hooks, notxml, nType = elem.nodeType; // don't get/set properties on text, comment and attribute nodes if ( !elem || nType === 3 || nType === 8 || nType === 2 ) { return; } notxml = nType !== 1 || !jQuery.isXMLDoc( elem ); if ( notxml ) { // Fix name and attach hooks name = jQuery.propFix[ name ] || name; hooks = jQuery.propHooks[ name ]; } if ( value !== undefined ) { if ( hooks && "set" in hooks && (ret = hooks.set( elem, value, name )) !== undefined ) { return ret; } else { return ( elem[ name ] = value ); } } else { if ( hooks && "get" in hooks && (ret = hooks.get( elem, name )) !== null ) { return ret; } else { return elem[ name ]; } } }

Ê×ÏÈ£¬ÎÒÃÇ¿´Ï attr() ºÍ prop() µÄÇø±ð£º

attr() ÀïÃ棬×î¹Ø¼üµÄÁ½ÐдúÂë

elem.setAttribute( name, value + "" ); ret = elem.getAttribute( name );

ºÜÃ÷ÏԵĿ´³öÀ´£¬Ê¹ÓÃµÄ DOM µÄ API setAttribute() ºÍ getAttribute() ·½·¨²Ù×÷µÄÊôÐÔÔªËؽڵ㡣

prop() ÀïÃ棬×î¹Ø¼üµÄÁ½ÐдúÂë

return ( elem[ name ] = value ); return elem[ name ];

¿ÉÒÔÀí½âΪ document.getElementById(el)[name] = value£¬ÕâÊÇת»¯³É element µÄÒ»¸öÊôÐÔ¡£

¶Ô±Èµ÷ÊÔ 1.9.0 ºÍ 1.8.3 µÄ attr() ·½·¨£¬·¢ÏÖÁ½ÕßµÄÇø±ðÔÚÓÚ

hooks.get( elem, name ))

·µ»ØµÄÖµ²»Ò»Ñù£¬¾ßÌåµÄʵÏÖ£º

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • ¡¶jQueryʵս¡·(jQueryʵս)ɨÃè°æ[PDF]

    ¡¶jQueryʵս¡·(jQueryʵս)ɨÃè°æ[PDF]

    2017-06-09 11:00

  • jQuery¼¼ÊõÁ·Ï°Ìâ

    jQuery¼¼ÊõÁ·Ï°Ìâ

    2017-06-09 10:00

  • ¼´¿ÌÌáÉýjQueryÐÔÄܵÄÊ®¸ö¼¼ÇÉ

    ¼´¿ÌÌáÉýjQueryÐÔÄܵÄÊ®¸ö¼¼ÇÉ

    2017-06-08 18:00

  • jQuery Mobile »ù´¡½Ì³Ì

    jQuery Mobile »ù´¡½Ì³Ì

    2017-06-08 17:05

ÍøÓѵãÆÀ
¸