Overview
Stores or reads data on an element, returning a jquery object.
When the parameter has only one key, the value corresponding to the key stored in the DOM is read by the jquery object, and it is worth noting that if the browser supports HTML5, it can also read the values stored in the DOM using data-[key] = [value]. See the last example.
When the parameter is two, the data for the Key-value key-value pair is stored in the DOM corresponding to the jquery object.
If the jquery collection points to multiple elements, the corresponding data is set on all elements. Instead of creating a new expando, this function can store data in any format on an element, not just a string.
V1.4.3 new usage, data (obj) can be passed in the form of Key-value.
Parameters
KeyString
V1.23
The stored data name.
Key,valueString,any
V1.2.3
key: the stored data name
value: Any data that will be stored
objObject
V1.4.3
A key/value pair for setting the data
data ()
V1.4.3Example Description:
Access data on a div
HTML Code:
<div></div>
JQuery Code:
$("div").data("blah"); // undefined$("div").data("blah", "hello"); // blah设置为hello$("div").data("blah"); // hello$("div").data("blah", 86); // 设置为86$("div").data("blah"); // 86$("div").removeData("blah"); //移除blah$("div").data("blah"); // undefined
Describe:
Save the name/value pair data on a div
HTML Code:
<div></div>
JQuery Code:
$("div").data("test", { first: 16, last: "pizza!" });$("div").data("test").first //16;$("div").data("test").last //pizza!;
Describe:
Read the pre-stored Data-[key] value in the div in the HTML5 specification
HTML Code:
<div data-test="this is test" ></div>
JQuery Code:
$("div").data("test"); //this is test!;
Removedata ([name|list]) Overview
Remove the stored data on the element
Contrary to the function of data ([key], [value])
Parameters
[Name]String
V1.2.3
The stored data name
[List]Array,string
V1.7
Move an array or a space-separated string
Example Description:
Delete the previously added data from the element:
JQuery Code:
$("#btn2").click(function(){ $("div").removeData("greeting"); alert("Greeting is: " + $("div").data("greeting"));});
Data ([Key],[value])