Data ([Key],[value])

Source: Internet
Author: User

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])

Related Article

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.