其实在jQuery1.5中已有这东西,cssHooks,或者更早之前的 jQuery.event.special, 或者 Sizzle.selectors里面更多的对象。它们共同的特点是包含了许多相关的函数,cssHooks是专门处理css属性的获取与设置,如IE的 opacity,event.special用于装载与卸载submit, change, focus ,mouseenter等特别事件与自定义事件, Sizzle.selectors里面的过滤器与候选集获取器就更不用说了。由于JS用对象做为表进行查找是比if条句与switch语句快很多,加之, 适配器这种模式对于扩展新功能非常有利,因为jQuery1.6便把它发扬光大了。
在jQuery的attributes模块(github是这样分割的,但耦合这么高很难说是模块),共增加了三个这样对 象,valHooks,attrHooks, propHooks,分别对应val,attr与prop这个三个方法。prop是新增的,表示jQuery决定区分属性与特性的决心,但IE6/7还是 无法区分它们,因此attr基本上涵盖了prop的功能。
我们看一下它们各自的运用吧!
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
|
// If a hook was provided get the non-computed value from there
|
if ( hooks && "get" in hooks && (ret = hooks.get( elem, false, extra )) !== undefined ) {
|
// Otherwise just get the value from the style object
|
if ( !arguments.length ) {
|
hooks = jQuery.valHooks[ elem.nodeName.toLowerCase() ] || jQuery.valHooks[ elem.type ];
|
if ( hooks && "get" in hooks && (ret = hooks.get( elem, "value" )) !== undefined ) {
|
return (elem.value || "").replace(rreturn, "");
|
//===============略============
|
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) ) {
|
hooks = jQuery.attrHooks[ name ] || ( jQuery.nodeName( elem, "form" ) && formHook );
|
if ( value !== undefined ) {
|
if ( value === null || (value === false && !rspecial.test( name )) ) {
|
jQuery.removeAttr( elem, name );
|
} else if ( hooks && "set" in hooks && notxml && (ret = hooks.set( elem, value, name )) !== undefined ) {
|
// Set boolean attributes to the same name
|
if ( value === true && !rspecial.test( name ) ) {
|
elem.setAttribute( name, "" + value );
|
if ( hooks && "get" in hooks && notxml ) {
|
return hooks.get( elem, name );
|
ret = elem.getAttribute( name );
|
// Non-existent attributes return null, we normalize to undefined
|
return ret === null ? undefined : ret;
|
hooks = jQuery.propHooks[ name ];
|
if ( value !== undefined ) {
|
if ( hooks && "set" in hooks && (ret = hooks.set( elem, value, name )) !== undefined ) {
|
return (elem[ name ] = value);
|
if ( hooks && "get" in hooks && (ret = hooks.get( elem, name )) !== undefined ) {
|
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 );
|
if ( !special.teardown || special.teardown.call( elem, namespaces ) === false ) {
|
jQuery.removeEvent( elem, type, elemData.handle );
|
可以发现还是很有章法的。这些适配器就是用来处理一些特殊的属性,样式或事件。而这些属性,样式或事件,我们可以通过浏览器的特征嗅探,把相应的解 决方法添加到适配器中。有了这些适配器,jQuery就可以省去许多if else 判定,当正式版发布时,又可以高兴地宣布这几个方法快了多少百分比了!