forked from jquery/api.jquery.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.xml
178 lines (176 loc) · 8.47 KB
/
data.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?xml version="1.0"?>
<entries>
<desc>Store arbitrary data associated with the matched elements or return the value at the named data store for the first element in the set of matched elements.</desc>
<entry type="method" name="data" return="jQuery">
<title>.data()</title>
<signature>
<added>1.2.3</added>
<argument name="key" type="String">
<desc>A string naming the piece of data to set.</desc>
</argument>
<argument name="value" type="Anything">
<desc>The new data value; this can be any Javascript type except <code>undefined</code>.</desc>
</argument>
</signature>
<signature>
<added>1.4.3</added>
<argument name="obj" type="Object">
<desc>An object of key-value pairs of data to update.</desc>
</argument>
</signature>
<desc>Store arbitrary data associated with the matched elements.</desc>
<longdesc>
<p>The <code>.data()</code> method allows us to attach data of any type to DOM elements in a way that is safe from circular references and therefore from memory leaks.</p>
<p>We can set several distinct values for a single element and retrieve them later:</p>
<pre><code>
$( "body" ).data( "foo", 52 );
$( "body" ).data( "bar", { myType: "test", count: 40 } );
$( "body" ).data( { baz: [ 1, 2, 3 ] } );
$( "body" ).data( "foo" ); // 52
$( "body" ).data(); // { foo: 52, bar: { myType: "test", count: 40 }, baz: [ 1, 2, 3 ] }
</code></pre>
<p>In jQuery 1.4.3 setting an element's data object with <code>.data(obj)</code> extends the data previously stored with that element.</p>
<p>Prior to jQuery 1.4.3 (starting in jQuery 1.4) the <code>.data()</code> method completely replaced all data, instead of just extending the data object. If you are using third-party plugins it may not be advisable to completely replace the element's data object, since plugins may have also set data.</p>
<p>Due to the way browsers interact with plugins and external code, the <code>.data()</code> method cannot be used on <code><object></code> (unless it's a Flash plugin), <code><applet></code> or <code><embed></code> elements.</p>
</longdesc>
<note id="no-data-on-xml" type="additional"/>
<note id="data-doesnt-accept-undefined" type="additional" data-title=".data" data-parameters=""name""/>
<example>
<desc>Store then retrieve a value from the div element.</desc>
<code><![CDATA[
$( "div" ).data( "test", { first: 16, last: "pizza!" } );
$( "span:first" ).text( $( "div" ).data( "test" ).first );
$( "span:last" ).text( $( "div" ).data( "test" ).last );
]]></code>
<css><![CDATA[
div {
color: blue;
}
span {
color: red;
}
]]></css>
<html><![CDATA[
<div>
The values stored were
<span></span>
and
<span></span>
</div>
]]></html>
</example>
<category slug="data"/>
<category slug="miscellaneous/data-storage"/>
<category slug="version/1.2.3"/>
<category slug="version/1.4"/>
<category slug="version/1.4.3"/>
</entry>
<entry type="method" name="data" return="Object">
<signature>
<added>1.2.3</added>
<argument name="key" type="String">
<desc>Name of the data stored.</desc>
</argument>
</signature>
<signature>
<added>1.4</added>
</signature>
<desc>Return the value at the named data store for the first element in the jQuery collection, as set by data(name, value) or by an HTML5 data-* attribute.</desc>
<longdesc>
<p>The <code>.data()</code> method allows us to attach data of any type to DOM elements in a way that is safe from circular references and therefore from memory leaks. We can retrieve several distinct values for a single element one at a time, or as a set:</p>
<pre><code>
alert( $( "body" ).data( "foo" ) );
alert( $( "body" ).data() );
</code></pre>
<p>The above lines alert the data values that were set on the <code>body</code> element. If no data at all was set on that element, <code>undefined</code> is returned.</p>
<pre><code>
alert( $( "body" ).data( "foo" ) ); // undefined
$( "body" ).data( "bar", "foobar" );
alert( $( "body" ).data( "bar" ) ); // foobar
</code></pre>
<h4 id="data-html5">
<a href="#data-html5">HTML5 data-* Attributes</a>
</h4>
<p>As of jQuery 1.4.3 <a href="http://ejohn.org/blog/html-5-data-attributes/">HTML 5 data- attributes</a> will be automatically pulled in to jQuery's data object. The treatment of attributes with embedded dashes was changed in jQuery 1.6 to conform to the <a href="http://www.w3.org/TR/html5/dom.html#embedding-custom-non-visible-data-with-the-data-*-attributes">W3C HTML5 specification</a>.</p>
<p>For example, given the following HTML:</p>
<pre><code><div data-role="page" data-last-value="43" data-hidden="true" data-options='{"name":"John"}'></div></code></pre>
<p>All of the following jQuery code will work.</p>
<pre><code>
$( "div" ).data( "role" ) === "page";
$( "div" ).data( "lastValue" ) === 43;
$( "div" ).data( "hidden" ) === true;
$( "div" ).data( "options" ).name === "John";
</code></pre>
<p>The second statement of the code above correctly refers to the <code>data-last-value</code> attribute of the element. In case no data is stored with the passed key, jQuery searches among the attributes of the element, converting a camel-cased string into a dashed string and then prepending <code>data-</code> to the result. So, the string <code>lastValue</code> is converted to <code>data-last-value</code>.</p>
<p>Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null). A value is only converted to a number if doing so doesn't change the value's representation. For example, "1E02" and "100.000" are equivalent as numbers (numeric value 100) but converting them would alter their representation so they are left as strings. The string value "100" is converted to the number 100.</p>
<p>When the data attribute is an object (starts with '{') or array (starts with '[') then <code>jQuery.parseJSON</code> is used to parse the string; it must follow <a href="http://en.wikipedia.org/wiki/JSON#Data_types.2C_syntax_and_example">valid JSON syntax</a> <em>including quoted property names</em>. If the value isn't parseable as a JavaScript value, it is left as a string.</p>
<p>To retrieve the value's attribute as a string without any attempt to convert it, use the <code><a href="/attr/">attr()</a></code> method.</p>
<p>The data- attributes are pulled in the first time the data property is accessed and then are no longer accessed or mutated (all data values are then stored internally in jQuery).</p>
<p>Calling <code>.data()</code> with no parameters retrieves all of the values as a JavaScript object. This object can be safely cached in a variable as long as a new object is not set with <code>.data(obj)</code>. Using the object directly to get or set values is faster than making individual calls to <code>.data()</code> to get or set each value:</p>
<pre><code>
var mydata = $( "#mydiv" ).data();
if ( mydata.count < 9 ) {
mydata.count = 43;
mydata.status = "embiggened";
}
</code></pre>
</longdesc>
<note id="no-data-on-xml" type="additional"/>
<example>
<desc>Get the data named "blah" stored at for an element.</desc>
<code><![CDATA[
$( "button" ).click(function() {
var value;
switch ( $( "button" ).index( this ) ) {
case 0 :
value = $( "div" ).data( "blah" );
break;
case 1 :
$( "div" ).data( "blah", "hello" );
value = "Stored!";
break;
case 2 :
$( "div" ).data( "blah", 86 );
value = "Stored!";
break;
case 3 :
$( "div" ).removeData( "blah" );
value = "Removed!";
break;
}
$( "span" ).text( "" + value );
});
]]></code>
<css><![CDATA[
div {
margin: 5px;
background: yellow;
}
button {
margin: 5px;
font-size: 14px;
}
p {
margin: 5px;
color: blue;
}
span {
color: red;
}
]]></css>
<html><![CDATA[
<div>A div</div>
<button>Get "blah" from the div</button>
<button>Set "blah" to "hello"</button>
<button>Set "blah" to 86</button>
<button>Remove "blah" from the div</button>
<p>The "blah" value of this div is <span>?</span></p>
]]></html>
</example>
<category slug="data"/>
<category slug="miscellaneous/data-storage"/>
<category slug="version/1.2.3"/>
<category slug="version/1.4"/>
<category slug="version/1.4.3"/>
</entry>
</entries>