Home > Web Front-end > JS Tutorial > Example of usage of data() method in jQuery_jquery

Example of usage of data() method in jQuery_jquery

WBOY
Release: 2016-05-16 16:24:14
Original
1143 people have browsed it

The example in this article describes the usage of the data() method in jQuery. Share it with everyone for your reference. The specific analysis is as follows:

This method can append data to the matching element, or get data from the matching element.

Grammar structure 1:

Copy code The code is as follows:
$(selector).data(name,value)

Parameter list:

参数 描述
name 存储的数据名称。
value 将要存储的任意数据。

Example code:

Copy code The code is as follows:






data() function-Script Home









The above code can append the name mydata to the matching div element, the value is "Script Home Welcomes You" data, and then use the data name to return.

Grammar structure 2:

Returns additional data with the specified data name from the matching element.

Copy code The code is as follows:
$(selector).data(name)

Parameter list:

参数 描述
name 存储的数据名称。

Example code:

Copy code The code is as follows:






data() function-Script Home









The above code can append the name mydata to the matching div element, the value is "Script Home Welcomes You" data, and then use the data name to return.

Grammar structure three:

Add data to matching elements using key/value pair objects.

Copy code The code is as follows:
$(selector).data(properties)

Parameter list:

参数 描述
properties 一个用于设置数据的键/值对。

Example code:

Copy code The code is as follows:






data() function-Script Home









The above code can append data named mydata to the div in the form of key/value pairs, and then obtain the attached data value through the data name and key.

Grammar structure four:

Use object method to append data to the specified element.

Copy code The code is as follows:
$(selector).data(object)

Parameter list:

参数 描述
object 一个用于设置数据的对象。

Example code:

Copy code The code is as follows:






data() function-Script Home









The above code appends data in the form of objects and obtains the attached data value.

I hope this article will be helpful to everyone’s jQuery programming.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template