This article mainly introduces three uses of Jquery-data. Has very good reference value. Let's take a look with the editor below, I hope it can help everyone.
Record
The use of Jquery-data:
jQuery-data is mainly used to store data and help ordinary objects or jQuery objects to store Data, in fact, if you simply store a single attribute of the dom, it is enough to use attr custom attributes; if you store multiple key-value pairs, it is recommended to use jQuery-data;
For example: Many plug-ins use this for lazy loading of images. When it comes to jquery-data, first store the real address of the picture in jquery-data, make a monitoring event, and then take out the real address when you slide to the picture;
Usage 1: Give to ordinary people The object stores a single attribute and value
<script type="text/javascript" src="jquery-3.0.0.min.js"></script> var obj = {}; $.data(obj, 'name', 'xm'); // 赋值 var str = $.data(obj, 'name'); // 读取值 console.log(str) // "xm"
Usage 2: Store multiple attributes and values for ordinary objects
<script type="text/javascript" src="jquery-3.0.0.min.js"></script> var obj = {}; $.data(obj,{name1:"xm",name2:"xh"}); // 赋值 var str1 = $.data(obj, 'name1'); // 读取值 var str2 = $.data(obj, 'name2'); // 读取值 console.log(str1) // "xm" console.log(str1) // "xh"
Usage 3: Assign value to Jquery dom object
<p class="demo"></p> <script type="text/javascript" src="jquery-3.0.0.min.js"></script> var obj = $('.demo'); $.data(obj,{name1:"xm",name2:"xh"}); // 赋值 var str1 = $.data(obj, 'name1'); // 读取值 var str2 = $.data(obj, 'name2'); // 读取值 console.log(str1) // "xm" console.log(str1) // "xh" // 就是把obj换成jquery对象这么简单
Related recommendations:
PHP How to solve the problem of Chinese garbled characters in MySQL stored data
Detailed introduction to stored data
html5 Detailed explanations of several examples of storing data on the client
The above is the detailed content of Analysis of three usage examples of Jquery-data. For more information, please follow other related articles on the PHP Chinese website!