In the official document of jQuery, users are prompted that this is a low-level method and should be replaced by the. data () method. $. Data (element, key, value) can append any type of data to the DOM element, but the memory leakage caused by loop reference should be avoided. The original Article is as follows:
The jQuery. data () method allows us to attach data of any type to DOM elements in a way that is safe from circular references and therefore from memory leaks. we can set several distinct values for a single element and retrieve them later:
However, this method is not only problematic. In jquery forum, this issue was discussed in depth. robert katic proposed a solution. The $. data () method is applied to the Host object and the running is optimized. However, if you use this method on the local image, the results may not be satisfactory. Under normal circumstances, an element can be deleted using the. remove () method and its data can be cleared. However, local objects cannot be completely deleted. The related data continues until the window object is closed. Similarly, these problems also exist in the event object, because the event processor (handlers) also uses this method for storage.
The simplest way to solve this problem is to store the data to a new attribute of the local object. That is:
//...
If (elem. nodeType ){
Cache [id] = dataObject;
Elem [expando] = id;
} Else {
Elem [expando] = dataObject;
}
//...
However, this method is powerless once inheritance is involved. Labels:
Var parent = {};
Var childA = Object. create (parent );
Var childB = Object. create (parent );
$. Data (parent, "foo", "parent value ");
// This may even be intentional
$. Data (childA, "foo") // => "parent value"
$. Data (childB, "foo") // => "parent value"
// This may NOT be intentional
$. Data (childA, "foo", "childA value ");
$. Data (parent, "foo") // => "childA value"
$. Data (childB, "foo") // => "childA value"
At the beginning, the object storing data does not exist. Therefore, an object is created to store new values,
Now, we try to modify the same data as the childA object.
- 2 pages in total:
- Previous Page
- 1
- 2
- Next Page