问题
Officially only $.cssHooks is documented in jQuery API documentation, and $.valHooks is mentioned in a sentence for a workaround to a known issue in .val().
I wonder how many hooks are there in jQuery besides of these 2, and should we use $.valHooks in our plugin development? If so, I think it should be documented as a dedicated topic instead of one-sentence only.
回答1:
There are 8 different types of exposed hooks.
There is one more for internal usage only (which is exposed) - jQuery._queueHooks().
There are also two other hooks: nodeHook and boolHook which are used internally and their functionality is basically embedded into jQuery.attrHooks and jQuery.valHooks.
Here are the important ones:
jQuery.attrHooks- Setting the type on a
radioinput after the value resets the value in IE6-9. - Set
contenteditableto false on removals (Bug #10429). - Set width and height to auto instead of 0 on empty string (Bug #8150)
- Some attributes require a special call on IE -
href,src,widthandheight. - Return
undefinedin the case of empty string for thestyleattribute. - Fix get/set for the
valueproperty in old IE - When reading attribute values if no hook is defined it uses either
boolHookornodeHook(which is the same asjQuery.valHooks.button). - Could be used to attach additional hooks when getting/setting an attribute of an element.
- Setting the type on a
jQuery.propHookshref/srcproperties should get the full normalized URL (Bug #10299, Bug #12915).- Fix for "Safari mis-reports the default selected property of an option".
- Could be used to attach additional hooks when getting/setting a property of an element.
jQuery.Tween.propHooksjQuery.Tweenis used for animations and such.- Fix for
scrollTopandscrollLeftin IE8 - will be removed in jQuery 2.0. - Other animation stuff.
- Fix for
jQuery.cssHooksAdd in style property hooks for overriding the default behavior of getting and setting a style property.
- Fix for getting/setting the value of the CSS property
opacity. - Fix for getting
witdthandheightdepending on thedisplayproperty. - Fix for setting
widthandheightdepending on thebox-sizingproperty. - Fix for getting margin-right.
- Fix for Webkit Bug #29084 -
getComputedStylereturns percent when specified for top/left/bottom/right - Used in
jQuery.fn.animateto expand specified style properties likemargin,paddingandborder. - Used in other various
jQuery.fn.animaterelated stuff. - Setting various background CSS properties to
inheritis achieved without usingjQuery.cssHooks, because it is shorter this way. - Could be used to attach additional hooks when getting/setting an individual style property of an element.
- Fix for getting/setting the value of the CSS property
jQuery.valHooks- Fixes a Blackberry 4.7 bug for
optionelements (Bug #6932). - Used for reading selected value for
selectelements. - Fixes reading some properties of the
buttonelement -id,nameandcoords. The same asnodeHook(see above). - Fixes the incompatibility on checking
radioandcheckboxinputs between Webkit and others. - Could be used to attach additional hooks when getting/setting the value of an input element.
- Fixes a Blackberry 4.7 bug for
jQuery.event.fixHooksA holder for
jQuery.event.mouseHooksandjQuery.event.keyHookswhich require fixing and normalizing based on the support of the current browser.jQuery.event.mouseHooks- Used to transfer some properties from the original
MouseEventto the jQuery event object. - Normalizes
event.which(which mouse button was used) accross browsers, becauseevent.buttonis not normalized. - Calculates
pageX,pageY,clientX,clientYand others if missing and set them to the jQuery event object.
- Used to transfer some properties from the original
jQuery.event.keyHooks- Used to transfer some properties from the original
KeyboardEventto the jQuery event object. - Normalizes
event.which(the character code of the button which was pressed) across browsers. In the original event it could bechar,charCode,keyorkeyCode.
- Used to transfer some properties from the original
From the jQuery 1.9.1 source.
来源:https://stackoverflow.com/questions/15494498/which-hooks-are-provided-in-jquery