JQuery Data Cache Usage Analysis
This article mainly introduces the usage of jQuery data cache and analyzes in detail the functions, definitions, and related usage skills of jQuery data cache. For more information, see
This article analyzes jQuery data cache usage. Share it with you for your reference. The details are as follows:
In the jQuery API help document, jQuery describes the role of data caching in this way: it is used to access data on an element to avoid the risk of circular reference.
I. Define cache data
You can use the $ (selector). data (name, value) method to define cache data for jQuery objects. The cached data is stored in all DOM elements in the matched DOM element set.
?
| 1 2 |
Var $ link = $ ('A '); $ Link. data ('linktype', 'home '); |
Note: $ (selector). data (name, value) can store data in any format on a matched DOM element, not just a string.
Ii. Obtain cache data
In this case, you only need a parameter that specifies the name of the cached data.
?
| 1 |
Var linkType = $ link. data ('linktype'); // 'home' |
Note: If the read cache data does not exist, the returned value is undefined. If the jQuery set points to multiple elements, only the corresponding cache data of the first element is returned.
Iii. Delete cache data
The removeData () function can delete the cached data of a specified name and return the corresponding jQuery object.
?
| 1 2 |
// When the cached data is deleted, the corresponding jQuery object is returned. Var $ a = $ link. removeData ('linktype '); |
Iv. usage specifications of jQuery data cache
As the number of calls to the data () function increases, or improperly used, the cache object expands sharply, and the program performance is eventually affected.
Therefore, when using the jQuery data cache function, cache objects should be promptly cleared. JQuery provides the removeData () function to manually clear cached data. According to the running mechanism of the jQuery framework, you do not need to manually clear the data cache in the following situations.
* If you perform the remove () operation on elem, jQuery will automatically clear the possible cache of the object.
* Perform the empty () operation on elem. If the current elem sub-element has a data cache, jQuery will also know the possible data cache of the sub-object.
* The clone () method of the jQuery replication node does not copy data cache.
I hope this article will help you with jQuery programming.