(v) JS Learning Note-jquery caching mechanism

Source: Internet
Author: User

Historical background

Development often because of the convenience, the status of flags are written to the DOM node, that is, htmlelement, disadvantages:

    • Circular references
    • Direct exposure to data, security?
    • Adding a bunch of custom attribute tags is meaningless to the browser
    • The HTML node should be manipulated when fetching data.

Fundamental purpose:

Use a low-coupling approach to connect the DOM to the cached data.

jquery now supports two types of:

$ ("Ele"). Data ()

1 JQuery.fn.extend ({2Datafunction(elem, name, data) {3             returnJquery.access ( This,function(value)) {4                 //The difference in each method is that each element is processed by a DOM node5                  This. each (function() {6                     7                 }  8             }9         }Ten         }, One........

Jquery.data (element, key, value)

 1   Jquery.extend ({ 2   AcceptData:Data.accepts,  3  HasData: function   (Elem) {},  4  //  Call the interface of the Data_user.access data class directly, The Elem entire jquery object is passed  5  data: function
     6  return   data_user.access (elem, name, data);  7   ",  8
       ... 

Difference:

Jquery.data (element, key, value)

Each element will have its own {Key:value} object that holds the data, so the new object will not overwrite the value corresponding to the original object key if it has the same key, because the new object is saved in another {Key:value} object

$ ("div"). Data ("A", "AAAA")

It is to bind data to each of the elements that match a div node

DEMO
<div id= "Test" >data test</div>vart1=$ ("#test");vart2=$ ("#test");//======= The first set of ========= $ (""). Data () methodT1.data (' A ', 1111); T2.data (' A ', 2222); T1.data (' A ')//result 222222, overwrittenT2.data (' a ')//result 222222//======= Second group ========= $.data () method$.data (T1,"B", "1111") $.data (T2,"B", "2222") $.data (T1,"B")//result 111111, not overwritten$.data (T2, "B")//result 222222

(v) JS Learning Note-jquery caching mechanism

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.