Latest web development tutorials

jQuery miscellaneous data () method

jQuery Misc Methods jQuery Misc Methods

Examples

To the <div> element additional data, and then retrieve the data:

$("#btn1").click(function(){
    $("div").data("greeting", "Hello World");
});
$("#btn2").click(function(){
    alert($("div").data("greeting"));
});

try it"

Definition and Usage

data () method to the selected additional data elements, or retrieve data from the selected elements.

Tip: To remove the data, use removeData () method.


Returns data from the elements

Returns additional data from the selected element.

grammar

$(selector).data(name)

参数 描述
name 可选。规定要取回的数据的名称。
如果没有规定名称,则该方法将以对象的形式从元素中返回所有存储的数据。


Additional data elements to

Additional data elements to be selected.

grammar

$(selector).data( name,value)

参数 描述
name 必需。规定要设置的数据的名称。
value 必需。规定要设置的数据的值。


Use objects to the additional data elements

Use with the name / value pairs to the selected additional data elements.

grammar

$(selector).data( object )

try it"

参数 描述
object 必需。规定包含名称/值对的对象。


jQuery Misc Methods jQuery Misc Methods