jQuery的数据缓存,静态字段jQuery.cache/jQuery.uuid/jQuery.expando
提供了data/removeData 存储/删除数据
jQuery.extend({ cache: {}, uuid: expando });
jQuery.cache 空对象,用来缓存
jQuery.uuid 在最新1.9中删除了
jQuery.expando 每一个复制的jQuery独特标志,去掉了非数字,用在data时在HTMLElement或js对象上标志
方法使用
data方法 为HTMLElement和js对象 提供缓存
// 为HTMLElement提供缓存 $('#testCache').data("test", {first:16, last: "pizza!"}); $('#testCache span:first').text($("#testCache").data("test").first); $('#testCache span:last').text($("#testCache").data("test").last); // 为js对象提供缓存 var myObj = {}; $.data(myObj, 'name', 'jack'); console.log( $.data(myObj, 'name') );
如何存储
1 如何在HTMLElement中存储缓存,数据最终存储在jQuery.cache中
<div id="testCache"></div> <script> var el = document.getElementById('testCache'); $.data(el, 'test', "value"); console.log(el[jQuery.expando]); // 1 console.log(jQuery.cache); // {1: {data: {test:"value"}}}; </script>
2 在js对象中缓存,js直接用一个key来存储缓存对象,key是jQuery.expando