Home  >  Article  >  Web Front-end  >  Analysis of three usage examples of Jquery-data

Analysis of three usage examples of Jquery-data

小云云
小云云Original
2018-01-17 15:06:201047browse

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, &#39;name&#39;, &#39;xm&#39;); // 赋值
 var str = $.data(obj, &#39;name&#39;); // 读取值
 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, &#39;name1&#39;); // 读取值
 var str2 = $.data(obj, &#39;name2&#39;); // 读取值
 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 = $(&#39;.demo&#39;);
 $.data(obj,{name1:"xm",name2:"xh"}); // 赋值
 var str1 = $.data(obj, &#39;name1&#39;); // 读取值
 var str2 = $.data(obj, &#39;name2&#39;); // 读取值
 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn