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