標籤:moved height setter pac 區別 ring 文檔 soft ble
很多同學在項目中都喜歡將資料存放區在HTMLElement屬性上,如
| 1234 |
<div data="some data">Test</div><script> div.getAttribute(‘data‘); // some data</script> |
給頁面中div添加了自訂屬性“data”及值“some data”。後續JS代碼中使用getAttribute擷取。
jQuery從1.2.3開始提供了data/removeData方法用來儲存/刪除資料。1.6.1程式碼片段
| 123456 |
jQuery.extend({ cache: {}, // Please use with caution uuid: 0, ...}); |
即給jQuery添加了靜態欄位/方法,有jQuery.cache/jQuery.uuid/jQuery.expando等。下面分別介紹
jQuery.cache Null 物件,用來緩衝。它的結構較複雜。
jQuery.uuid 自增唯一的數字。
jQuery.expando 字串,使用Math.random產生,去掉了非數字字元。它作為HTMLElement或JS對象的屬性名稱。
| 1 |
expando: "jQuery" + ( jQuery.fn.jquery + Math.random() ).replace( /\D/g, "" ), |
jQuery.noData JS對象,對於指定的HTMLElement禁用data方法。如embed、applet。
jQuery.hasData 用來判斷HTMLElement或JS對象是否具有資料。返回true或false。即如果調用了jQuery.data方法添加了屬性,則返回true。
| 1234567 |
<div>aa</div><script> var div = document.getElementsByTagName(‘div‘)[0]; $.hasData(div); // false $.data(div, ‘name‘,‘jack‘); $.hasData(div); // true</script> |
jQuery.acceptData 用來判斷該元素是否能接受資料,返回true或false。在jQuery.data中使用。
jQuery.data 這是提供給用戶端程式員使用的方法,它同時是setter/getter。
- 傳一個參數,返回附加在指定元素的所有資料,即thisCachejQuery.data(el); // thisCache
- 傳二個參數,返回指定的屬性值jQuery.data(el, ‘name‘);
- 傳三個參數,設定屬性及屬性值jQuery.data(el, ‘name‘, ‘jack‘);jQuery.data(el, ‘uu‘, {});
- 傳四個參數,第四個參數pvt僅提供給jQuery庫自身使用。即jQuery._data方法中傳true。因為jQuery的事件模組嚴重依賴於jQuery.data,為避免人為的不小心重寫在這個版本中加入的
jQuery.removeData 刪除資料。
上面是jQuery資料緩衝模組的整體概述,下面詳細說下jQuery.data方法。jQuery.data為兩種對象提供緩衝:JS對象和HTMLElement
| 123456789101112 |
// 為JS對象提供緩衝var myObj = {};$.data(myObj, ‘name‘, ‘jack‘);$.data(myObj, ‘name‘); // jack // 為HTMLElement提供緩衝<div id="xx"></div><script> var el = document.getElementById(‘xx‘); $.data(el, ‘name‘, ‘jack‘); $.data(el, ‘name‘); // jack</script> |
內部實現上也是有區別的,
1,為JS對象提供緩衝時,直接將資料儲存在JS對象上。cache為JS對象。此時會偷偷的給JS對象添加個屬性(類似於jQuery16101803968874529044),屬性值也是個JS對象。舉例說明
| 123 |
var myObj = {};$.data(myObj, ‘name‘, ‘jack‘);console.log(myObj); |
myObj的結構如下
| 12345 |
myObj = { jQuery16101803968874529044 : { name : ‘jack‘ }} |
“jQuery16101803968874529044”這個字串在data內部命名為id(注意並非HTMLElement元素的id),它實際就是jQuery.expando。上面已經提到它是在jQuery.js引入到頁面後隨機產生的。
2,為HTMLElement提供緩衝時,卻不會直接儲存在HTMLElement上。而是儲存在jQuery.cache上。cache為jQuery.cache。此時先給HTMLElement添加屬性(類似於jQuery16101803968874529044),屬性值為數字(1,2,3遞增)。即只將一些數字儲存在了HTMLElement上,不會直接將資料置入。這是因為IE老版本中可能會存在記憶體泄露危險。而HTMLElement如何與jQuery.cache建立聯絡呢? 還是id。剛剛提到屬性值數字就是id。舉例說明
| 1234567 |
<div id="xx"></div><script> var el = document.getElementById(‘xx‘); $.data(el, ‘name‘, ‘jack‘); console.log(el[jQuery.expando]); // 1 console.log(jQuery.cache); // {1 : {name:‘jack‘}}</script> |
el 上添加了屬性jQuery.expando,值為id,這個id是從1開始遞增的。而id又作為jQuery.cache的屬性(key)。這樣就HTMLElement就與jQuery.cache建立了聯絡。
不知注意到沒有,jQuery.data還有第四個參數pvt,這個參數只在jQuery._data中使用。
| 1234 |
// For internal use only._data: function( elem, name, data ) { return jQuery.data( elem, name, data, true );}, |
jQuery._data從命名上就指定它是私人的,使用jQuery的用戶端程式員不應該去調用該方法。jQuery的API文檔上也不會公開它。
jQuery的資料緩衝模組從1.2.3到1.6.1幾乎每個版本都在變。jQuery._data的提出就是為了避免用戶端程式員覆蓋/重寫了默寫模組。如jQuery事件模組中事件handler等就使用jQuery.data儲存,如果重寫了該模組。那麼事件模組將癱瘓。因此特意添加了pvt參數及jQuery._data方法。
但如果你刻意要破壞,那麼還是可以做的。如下
| 12345678910 |
<div id="xx">Test</div><script> $(‘#xx‘).click(function(){ alert(‘click‘); }); // 語句1 $.data($(‘#xx‘)[0], ‘events‘, ‘‘, true); // 語句2 //$._data($(‘#xx‘)[0], ‘events‘, ‘‘);</script> |
點擊div[id=xx]將不會觸發點擊事件。
整個jQuery.data設定(set)資料緩衝的過程就是如此,理解的這個。取資料(get)的過程就好理解了。不重複。
最後,我會給zChian.js添加zChain.data/removeData方法,因為是“迷你版”,僅給HTMLElement添加資料緩衝。請注意。
jQuery快取資料