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