New adapter for jQuery1.6c

Source: Internet
Author: User

In fact, this is already available in jQuery1.5, cssHooks, jQuery. event. special earlier, or more objects in Sizzle. selectors. Their common characteristics are that they contain many related functions. cssHooks is specialized in obtaining and setting css attributes, such as opacity and event of IE. special is used to mount and unmount special events and custom events such as submit, change, focus, and mouseenter, Sizzle. let alone the filters and candidate set collectors in selectors. As JS uses objects as tables for search, it is much faster than if and switch statements. In addition, the adapter mode is very advantageous for the expansion of new functions, because jQuery1.6 has carried forward it.

In jQuery's attributes module (github is divided in this way, but it is hard to say that coupling is so high), three such objects are added: valHooks, attrHooks, and propHooks, which correspond to val, respectively, attr and prop. Prop is newly added, indicating jQuery's determination to differentiate attributes and features, but IE6/7 still cannot distinguish them, so attr basically covers the prop function.

Let's take a look at their respective applications!

 
 
  1. // JQuery. style Method
  2. If (value! = Undefined ){
  3. // =====================================
  4. // If a hook was provided, use that value, otherwise just set the specified value
  5. If (! Hooks |! ("Set" in hooks) | (value = hooks. set (elem, value ))! = Undefined ){
  6. // Wrapped to prevent IE from throwing errors when 'invalid' values are provided
  7. // Fixes bug #5509
  8. Try {
  9. Style [name] = value;
  10. } Catch (e ){}
  11. }
  12. } Else {
  13. // If a hook was provided get the non-computed value from there
  14. If (hooks & "get" in hooks & (ret = hooks. get (elem, false, extra ))! = Undefined ){
  15. Return ret;
  16. }
  17. // Otherwise just get the value from the style object
  18. Return style [name];
  19. }
 
 
  1. // JQuery. fn. val Method
  2. If (! Arguments. length ){
  3. If (elem ){
  4. Hooks = jQuery. valHooks [elem. nodeName. toLowerCase ()] | jQuery. valHooks [elem. type];
  5. If (hooks & "get" in hooks & (ret = hooks. get (elem, "value "))! = Undefined ){
  6. Return ret;
  7. }
  8. Return (elem. value | ""). replace (rreturn ,"");
  9. }
  10. Return undefined;
  11. }
  12. // ===============================
  13. Hooks = jQuery. valHooks [this. nodeName. toLowerCase ()] | jQuery. valHooks [this. type];
  14. // If set returns undefined, fall back to normal setting
  15. If (! Hooks | ("set" in hooks & hooks. set (this, val, "value") === undefined )){
  16. This. value = val;
  17. }
  18.  
 
 
  1. // JQuery. attr Method
  2. Hooks = jQuery. attrHooks [name] | (jQuery. nodeName (elem, "form") & formHook );
  3. If (value! = Undefined ){
  4. If (value = null | (value = false &&! Rspecial. test (name ))){
  5. JQuery. removeAttr (elem, name );
  6. Return undefined;
  7. } Else if (hooks & "set" in hooks & notxml & (ret = hooks. set (elem, value, name ))! = Undefined ){
  8. Return ret;
  9. } Else {
  10. // Set boolean attributes to the same name
  11. If (value = true &&! Rspecial. test (name )){
  12. Value = name;
  13. }
  14. Elem. setAttribute (name, "" + value );
  15. Return value;
  16. }
  17. } Else {
  18. If (hooks & "get" in hooks & notxml ){
  19. Return hooks. get (elem, name );
  20. } Else {
  21. Ret = elem. getAttribute (name );
  22. // Non-existent attributes return null, we normalize to undefined
  23. Return ret = null? Undefined: ret;
  24. }
  25. }
  26.  
 
 
  1. // JQuery. prop Method
  2. Hooks = jQuery. propHooks [name];
  3. If (value! = Undefined ){
  4. If (hooks & "set" in hooks & (ret = hooks. set (elem, value, name ))! = Undefined ){
  5. Return ret;
  6. } Else {
  7. Return (elem [name] = value );
  8. }
  9. } Else {
  10. If (hooks & "get" in hooks & (ret = hooks. get (elem, name ))! = Undefined ){
  11. Return ret;
  12. } Else {
  13. Return elem [name];
  14. }
  15. }
 
 
  1. //jQuery.event.add    
  2.       if ( !special.setup || special.setup.call( elem, data, namespaces, eventHandle ) === false ) {    
  3.      // Bind the global event handler to the element    
  4.         if ( elem.addEventListener ) {    
  5.           elem.addEventListener( type, eventHandle, false );   
  6.         } else if ( elem.attachEvent ) {    
  7.           elem.attachEvent( "on" + type, eventHandle );    
  8.         }    
  9.       }    
  10. //jQuery.event.remove    
  11.       if ( !special.teardown || special.teardown.call( elem, namespaces ) === false ) {    
  12.        jQuery.removeEvent( elem, type, elemData.handle );    
  13.    }   

It can be found that there are still many rules. These adapters are used to process special attributes, styles, or events. These attributes, styles, or events can be added to the adapter through browser feature sniffing. With these adapters, jQuery can save a lot of if else judgments. When the official version is released, we can happily announce the percentage of these methods!

It can be found that there are still many rules. These adapters are used to process special attributes, styles, or events. These attributes, styles, or events can be added to the adapter through browser feature sniffing. With these adapters, jQuery can save a lot of if else judgments. When the official version is released, we can happily announce the percentage of these methods!

It can be found that there are still many rules. These adapters are used to process special attributes, styles, or events. These attributes, styles, or events can be added to the adapter through browser feature sniffing. With these adapters, jQuery can save a lot of if else judgments.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.