The use of Jquery-data:
Jquery-data is mainly used to store data, to help ordinary objects or jquery objects to store data, in fact, if the simple storage of the DOM single attribute, with attr custom attributes enough, if the storage of multiple key value pairs is recommended jquery-data;
For example: The image of lazy loading a lot of plug-ins to use the Jquery-data, first the real address of the picture stored in the Jquery-data, do a listening event, until the slide to the image of the real address to take out;
Usage One: storing individual attributes and values for normal objects
1 < type= "Text/javascript" src= "Jquery-3.0.0.min.js"></ Script > 2 var obj = {}; 3 $.data (obj, ' name ', ' XM ');//Assignment 4 var str = $.data (obj, ' name ');//Read Value 5 Console.log (str)//"XM"
Usage two: Store multiple attributes and values for a normal object
<type= "Text/javascript" src= "Jquery-3.0.0.min.js"> </script>var obj = {}; $.data (obj,{name1: "XM", Name2: "XH"});//assignment var str1 = $.data (obj , ' name1 '); Read Value var str2 = $.data (obj, ' name2 ');//Read Value Console.log (STR1)//"XM" Console.log (STR1)//"XH"
Usage three: Assigning values to jquery DOM objects
<class= "Demo"></div>< type= "Text/javascript" src= "Jquery-3.0.0.min.js"></ script>var obj = $ ('. Demo '); $.data (obj,{name1: "XM", Name2: "XH"});//assignment var str1 = $ . Data (obj, ' name1 '); Read Value var str2 = $.data (obj, ' name2 ');//Read Value Console.log (STR1)//"XM" Console.log (STR1)//"XH"//is to replace obj with jquery object So Simple
Three ways to use Jquery-data