代码之家  ›  专栏  ›  技术社区  ›  Matrym

jQuery数据函数

  •  0
  • Matrym  · 技术社区  · 16 年前

    它是否比给dom元素一个类或属性更有效? 数据存储在哪里?

    PS:我曾经看到一个链接,指向一个页面,该页面执行jquery的方法搜索,但是我丢失了它

    3 回复  |  直到 10 年前
        1
  •  1
  •   Sniffer    16 年前
        2
  •  1
  •   Sean Vieira    16 年前

    我相信你要找的是 James Padolsey's jQuery SourceCode viewer

    // The following elements throw uncatchable exceptions if you
        // attempt to add expando properties to them.
        noData: {
            "embed": true,
            "object": true,
            "applet": true
        },
    
        data: function( elem, name, data ) {
            if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
                return;
            }
    
            elem = elem == window ?
                windowData :
                elem;
    
            var id = elem[ expando ], cache = jQuery.cache, thisCache;
    
            if ( !id && typeof name === "string" && data === undefined ) {
                return null;
            }
    
            // Compute a unique ID for the element
            if ( !id ) { 
                id = ++uuid;
            }
    
            // Avoid generating a new cache unless none exists and we
            // want to manipulate it.
            if ( typeof name === "object" ) {
                elem[ expando ] = id;
                thisCache = cache[ id ] = jQuery.extend(true, {}, name);
    
            } else if ( !cache[ id ] ) {
                elem[ expando ] = id;
                cache[ id ] = {};
            }
    
            thisCache = cache[ id ];
    
            // Prevent overriding the named cache with undefined values
            if ( data !== undefined ) {
                thisCache[ name ] = data;
            }
    
            return typeof name === "string" ? thisCache[ name ] : thisCache;
        },
    
        removeData: function( elem, name ) {
            if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
                return;
            }
    
            elem = elem == window ?
                windowData :
                elem;
    
            var id = elem[ expando ], cache = jQuery.cache, thisCache = cache[ id ];
    
            // If we want to remove a specific section of the element's data
            if ( name ) {
                if ( thisCache ) {
                    // Remove the section of cache data
                    delete thisCache[ name ];
    
                    // If we've removed all the data, remove the element's cache
                    if ( jQuery.isEmptyObject(thisCache) ) {
                        jQuery.removeData( elem );
                    }
                }
    
            // Otherwise, we want to remove all of the element's data
            } else {
                if ( jQuery.support.deleteExpando ) {
                    delete elem[ jQuery.expando ];
    
                } else if ( elem.removeAttribute ) {
                    elem.removeAttribute( jQuery.expando );
                }
    
                // Completely remove the data cache
                delete cache[ id ];
            }
        }
    
        3
  •  0
  •   Community Mohan Dere    9 年前

    在使用本软件之前,您需要注意一些规则!

    添加

    添加对象会将对象放置在数据对象内,并“扩展以前与该元素一起存储的数据”- http://api.jquery.com/data/#entry-longdesc

    这意味着将obj添加到dataObj

    dataObj === { /*previous data*/, obj : { } }
    

    使用

    如果您存储了简单的值,则可以将它们放入变量中,并在不更改数据对象的情况下对它们执行所需的操作。

    如果使用对象或数组在元素上存储数据,请小心!

    仅仅因为将其存储到变量中并不意味着不更改数据值。

    这就是它的本来面目。。除非很简单。。那只是一份拷贝。:p页

    var data             = $("#id").data();  // Get a reference to the data object 
    data.r_redirect      = "index.php";      // Add a string value
    data.num             = 0;                // Add a integer value
    data.arr             = [0,1,2];          // Add an array
    data.obj             = { a : "b" };      // Add an object
    
                                             // but here is where the fun starts! 
    
    var r_redirectString = data.r_redirect;  // returns "index.php", as expected.. cool
    r_redirectString     = "changed"         // change the value and the compare :
    data.r_redirect      == r_redirectString // returns false, the values are different
    
    var oArr             = data.arr;         // Now lets copy this array
    oArr.push(3);                            // and modify it.
    data.arr            == oArr              // should be false? Nope. returns true.
                                             // arrays are passed by reference.
                                             // but..
    
    var oObj             = data.obj          // what about objects?       
    oObj["key"]          = "value";          // modify the variable and
    data.obj["key"]     == oObj["key"]       // it returns true, too!
    

    所以,资源。。

    What's the best way to store multiple values for jQuery's $.data()? https://stackoverflow.com/a/5759883/1257652