The main methods are as follows:
Copy Code code as follows:
. Data (key, value)
. Data (obj)
. Data (Key)
. Data ()
From jquery 1.4.3, the HTML 5 data-attribute is automatically referenced to the data object in jquery.
For example, HTML:
Copy Code code as follows:
<div data-role = "page" Data-last-value = "Data-hidden =" true "Data-options = ' {' name ': ' John '} ' > </div>
The following jquery code returns true:
Copy Code code as follows:
$ ("div"). Data ("role") = = "Page";
$ ("div"). Data ("lastvalue") = = 43;
$ ("div"). Data ("hidden") = = true;
$ ("div"). Data ("Options"). Name = = = "John";
Unlike the HTML5 API, jquery attempts to convert a string to a JavaScript value (including Boolean values (Booleans), Numbers (numbers), Objects (objects), arrays (arrays), and null (NULL). If this does not change the numeric representation, the value is converted to a number. For example, "1E02" and "100.000" are equivalent to numbers (numeric value 100), but will convert them to change their representations, so they are reserved as strings. The string value "100" is converted to the number 100.
If the Data property is an object (starting with "{") or an array (starting with ' ['), you can parse it into a string using Jquery.parsejson, and it must follow valid JSON syntax, including a property name with double quotes. If the value cannot be resolved to a JavaScript value, it is preserved as a string.
Use the attr () method if you want to take the property values that you removed directly as strings.
The Data-property is no longer accessed or changed after the first use of this data attribute (all data values are stored inside jquery).
When the. Data () is invoked without arguments, all data is fetched as a JavaScript object. This object can be safely stored in a variable, because once the new object is extracted, the. Data (obj) operation on the element will no longer affect the object. In addition, direct manipulation of this object can be faster than setting or getting values per call to. Data ().