The data () method is used in jquery to read the HTML5 custom attribute data-* instance _jquery

Source: Internet
Author: User
Tags numeric value

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 ().

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.