Historical background
Development often because of the convenience, the status of flags are written to the DOM node, that is, htmlelement, disadvantages:
- Circular references
- Direct exposure to data, security?
- Adding a bunch of custom attribute tags is meaningless to the browser
- The HTML node should be manipulated when fetching data.
Fundamental purpose:
Use a low-coupling approach to connect the DOM to the cached data.
jquery now supports two types of:
$ ("Ele"). Data ()
1 JQuery.fn.extend ({2Datafunction(elem, name, data) {3 returnJquery.access ( This,function(value)) {4 //The difference in each method is that each element is processed by a DOM node5 This. each (function() {6 7 } 8 }9 }Ten }, One........
Jquery.data (element, key, value)
1 Jquery.extend ({ 2 AcceptData:Data.accepts, 3 HasData: function (Elem) {}, 4 // Call the interface of the Data_user.access data class directly, The Elem entire jquery object is passed 5 data: function
6 return data_user.access (elem, name, data); 7 ", 8
...
Difference:
Jquery.data (element, key, value)
Each element will have its own {Key:value} object that holds the data, so the new object will not overwrite the value corresponding to the original object key if it has the same key, because the new object is saved in another {Key:value} object
$ ("div"). Data ("A", "AAAA")
It is to bind data to each of the elements that match a div node
DEMO
<div id= "Test" >data test</div>vart1=$ ("#test");vart2=$ ("#test");//======= The first set of ========= $ (""). Data () methodT1.data (' A ', 1111); T2.data (' A ', 2222); T1.data (' A ')//result 222222, overwrittenT2.data (' a ')//result 222222//======= Second group ========= $.data () method$.data (T1,"B", "1111") $.data (T2,"B", "2222") $.data (T1,"B")//result 111111, not overwritten$.data (T2, "B")//result 222222
(v) JS Learning Note-jquery caching mechanism