jquery Data Caching usage Analysis _jquery

Source: Internet
Author: User

This paper analyzes the usage of jquery data caching. Share to everyone for your reference. Specifically as follows:

In jquery's API Help document, jquery describes the role of data caching as a way to access data on one element to avoid the risk of circular references.

First, define cached data

Use the $ (selector). Data (Name,value) method to define cached data for jquery objects. These cached data are stored in all DOM elements in the collection of matching DOM elements.

var $link = $ (' a ');
$link. Data (' linktype ', ' home ');

Note: $ (selector). Data (Name,value) allows you to store any format on a matching DOM element, not just a string.

Second, get cached data

At this point, you need only one parameter that specifies the name of the cached data.

var linktype = $link. Data (' linktype '); ' Home '

Note: If the cached data read does not exist, the return value is undefined; if the jquery collection points to multiple elements, only the corresponding cached data for the first element will be returned.

Third, delete cached data

The Removedata () function deletes cached data for the specified name and returns the corresponding JQuery object.

When the cached data is deleted, the corresponding jquery object is returned.
var $a = $link. Removedata (' linktype ');  

Four, the jquery data Cache usage Specification

With the number of calls to the data () function increased, or because of improper use, it will cause the cache object to expand dramatically, ultimately affecting the performance of the program.
So when using the jquery data caching feature, you should clean up cached objects in a timely manner. jquery provides the Removedata () function to manually purge cached data. Depending on the operating mechanism of the jquery framework, the following situations do not require manual cleanup of the data cache.

* The Remove () action is performed on the Elem, and jquery automatically clears the cache that the object may exist.
* Perform a empty () operation on Elem, and jquery also knows the possible data caches of child objects if there is a data cache for the current elem child element.
* The Clone () method of the jquery replication node does not replicate the data cache.

I hope this article will help you with your 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.