JQuery Data Cache Usage Analysis

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.