Latest web development tutorials

jQuery.data () method

jQuery Misc Methods jQuery Misc Methods

Examples

On the div element to store and then retrieve data

<Div> value stored in the <span> </ span> and <span> </ span> < / div> <script> $ (function () { var div = $ ( "Div" ) [ 0 ];. JQuery data ( div, "test", { first: 16, last: "pizza !" }); $ ( "Span: first" ). Text ( jQuery. data ( div, "test" ). First ); $ ( "Span: last" ). Text ( jQuery. data ( div, "test" ). Last );}) </ Script>

try it"

Definition and Usage

$ .data () Function is used to access data on the specified elements, returns the set value.

Note: 1. This is a low-level method, .data () method is more convenient to use.
2. () function to access data are temporary data through data, refresh the page once, before storing the data will be removed.
3. This method is currently not available on XML cross-platform document settings, Internet Explorer is not allowed in XML documents through custom attributes additional data.


grammar

Use a

$.data( element, key, value )

Note: 1. () function to access data are temporary data through data, refresh the page once, before storing the data will be removed.
2. undefined is a data value is not recognized. Call jQuery.data (el, "name", undefined) returns the corresponding "name" data, equivalent to jQuery.data (el, "name").

We can set different values ​​on an element, and get these values:

jQuery.data(document.body, 'foo', 52);
jQuery.data(document.body, 'bar', 'test');

Use two

$.data( element, key )

We can set different values ​​on an element, and get these values:

alert(jQuery.data( document.body, 'foo' ));
alert(jQuery.data( document.body ));

参数 描述
element Element类型 要存储数据的DOM对象
key 可选。String类型 指定的键名字符串。
value 可选。 Object类型 需要存储的任意类型的数据。


Examples

More examples

retrieve data
It is stored in the element named "blah" data.


jQuery Misc Methods jQuery Misc Methods