$.extend({ // jQuery.removeData(elem,name,pvt)用于移除通过jQuery.data()设置的数据 removeData: function( elem, name, pvt /* Internal Use Only */ ) { if ( !jQuery.acceptData( elem ) ) { return; } var thisCache, i, l, // Reference to internal data cache key internalKey = jQuery.expando, isNode = elem.nodeType, // See jQuery.data for more information cache = isNode ? jQuery.cache : elem, // See jQuery.data for more information id = isNode ? elem[ internalKey ] : internalKey; // If there is already no cache entry for this object, there is no // purpose in continuing if ( !cache[ id ] ) { return; } // 如果传入参数name, 则移除一个或多个数据 if ( name ) { thisCache = pvt ? cache[ id ] : cache[ id ].data; if ( thisCache ) { // 只有数据缓存对象thisCache存在时,才有必要移除数据 // Support array or space separated string names for data keys if ( !jQuery.isArray( name ) ) { // try the string as a key before any manipulation if ( name in thisCache ) { name = [ name ]; } else { // split the camel cased version by spaces unless a key with the spaces exists name = jQuery.camelCase( name ); if ( name in thisCache ) { name = [ name ]; } else { name = name.split( " " ); } } } // 遍历参数name中的数据名,用运算符delete逐个从数据缓存对象thisCache中移除 for ( i = 0, l = name.length; i < l; i++ ) { delete thisCache[ name[i] ]; } // If there is no data left in the cache, we want to continue // and let the cache object itself get destroyed if ( !( pvt ? isEmptyDataObject : jQuery.isEmptyObject )( thisCache ) ) { return; } } } // See jQuery.data for more information // 删除自定义数据缓存对象cache[id].data if ( !pvt ) { delete cache[ id ].data; // Don't destroy the parent cache unless the internal data object // had been the only thing left in it if ( !isEmptyDataObject(cache[ id ]) ) { return; } } // Browsers that fail expando deletion also refuse to delete expandos on // the window, but it will allow it on all other JS objects; other browsers // don't care // Ensure that `cache` is not a window object #10080 // 删除数据缓存对象cache[id] if ( jQuery.support.deleteExpando || !cache.setInterval ) { delete cache[ id ]; } else { cache[ id ] = null; } // We destroyed the cache and need to eliminate the expando on the node to avoid // false lookups in the cache for entries that no longer exist // 删除DOM元素上扩展的jQuery.expando属性 if ( isNode ) { // IE does not allow us to delete expando properties from nodes, // nor does it have a removeAttribute function on Document nodes; // we must handle all of these cases if ( jQuery.support.deleteExpando ) { delete elem[ internalKey ]; } else if ( elem.removeAttribute ) { elem.removeAttribute( internalKey ); } else { elem[ internalKey ] = null; } } } }); jQuery.fn.extend({ removeData: function( key ) { return this.each(function() { jQuery.removeData( this, key ); }); } }); // checks a cache object for emptiness function isEmptyDataObject( obj ) { for ( var name in obj ) { // if the public data object is empty, the private is still empty if ( name === "data" && jQuery.isEmptyObject( obj[name] ) ) { continue; } if ( name !== "toJSON" ) { return false; } } return true; }
六、$.hasData(elem)
使用方法:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery.hasData demo</title> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> </head> <body> <p>Results: </p> <script> var $p = jQuery( "p" ), p = $p[ 0 ]; $p.append( jQuery.hasData( p ) + " " ); // false $.data( p, "testing", 123 ); $p.append( jQuery.hasData( p ) + " " ); // true $.removeData( p, "testing" ); $p.append( jQuery.hasData( p ) + " " ); // false $p.on( "click", function() {} ); $p.append( jQuery.hasData( p ) + " " ); // true $p.off( "click" ); $p.append( jQuery.hasData( p ) + " " ); // false </script> </body> </html> $.hasData(elem) 源码解析: $.extend({ hasData: function( elem ) { elem = elem.nodeType ? jQuery.cache[ elem[jQuery.expando] ] : elem[ jQuery.expando ]; return !!elem && !isEmptyDataObject( elem ); // 如果关联的数据缓存对象存在,并且含有数据,则返回true, 否则返回false。 这里用两个逻辑非运算符! 把变量elem转换为布尔值 } });
您可能感兴趣的文章: