1 line
4.4 KiB
JSON
1 line
4.4 KiB
JSON
{"name":"jQuery.data","type":"method","title":"jQuery.data()","deprecated":null,"removed":null,"desc":"Store arbitrary data associated with the specified element and/or return the value that was set.","categories":["data","utilities","version/1.2.3","version/1.4","version/1.4.3"],"entries":[{"return":"Object","signatures":{"added":"1.2.3","argument":[{"desc":"The DOM element to associate with the data.","name":"element","type":"Element"},{"desc":"A string naming the piece of data to set.","name":"key","type":"String"},{"desc":"The new data value.","name":"value","type":"Object"}]},"examples":{"desc":"Store then retrieve a value from the div element.","code":"\nvar div = $( \"div\" )[ 0 ];\njQuery.data( div, \"test\", {\n first: 16,\n last: \"pizza!\"\n});\n$( \"span:first\" ).text( jQuery.data( div, \"test\" ).first );\n$( \"span:last\" ).text( jQuery.data( div, \"test\" ).last );\n","css":"\n div {\n color: blue;\n }\n span {\n color: red;\n }\n","html":"\n<div>\n The values stored were\n <span></span>\n and\n <span></span>\n</div>\n"},"desc":"Store arbitrary data associated with the specified element. Returns the value that was set.","longdesc":"\n <p><strong>Note:</strong> This is a low-level method; a more convenient <code><a href=\"/data/\">.data()</a></code> is also available.</p>\n <p>The <code>jQuery.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 free from memory leaks. jQuery ensures that the data is removed when DOM elements are removed via jQuery methods, and when the user leaves the page. We can set several distinct values for a single element and retrieve them later:</p>\n <pre><code>\njQuery.data( document.body, \"foo\", 52 );\njQuery.data( document.body, \"bar\", \"test\" );\n </code></pre>\n <p><em>Note:</em> this method currently does not provide cross-platform support for setting data on XML documents, as Internet Explorer does not allow data to be attached via expando properties.</p>\n "},{"return":"Object","signatures":[{"added":"1.2.3","argument":[{"desc":"The DOM element to query for the data.","name":"element","type":"Element"},{"desc":"Name of the data stored.","name":"key","type":"String"}]},{"added":"1.4","argument":{"desc":"The DOM element to query for the data.","name":"element","type":"Element"}}],"examples":{"desc":"Get the data named \"blah\" stored at for an element.","code":"\n$( \"button\" ).click( function() {\n var value,\n div = $( \"div\" )[ 0 ];\n switch ( $( \"button\" ).index( this ) ) {\n case 0 :\n value = jQuery.data( div, \"blah\" );\n break;\n case 1 :\n jQuery.data( div, \"blah\", \"hello\" );\n value = \"Stored!\";\n break;\n case 2 :\n jQuery.data( div, \"blah\", 86 );\n value = \"Stored!\";\n break;\n case 3 :\n jQuery.removeData( div, \"blah\" );\n value = \"Removed!\";\n break;\n }\n $( \"span\" ).text( \"\" + value );\n});\n","css":"\n div {\n margin: 5px;\n background: yellow;\n }\n button {\n margin: 5px;\n font-size: 14px;\n }\n p {\n margin: 5px;\n color: blue;\n }\n span {\n color: red;\n }\n","html":"\n<div>A div</div>\n<button>Get \"blah\" from the div</button>\n<button>Set \"blah\" to \"hello\"</button>\n<button>Set \"blah\" to 86</button>\n<button>Remove \"blah\" from the div</button>\n<p>The \"blah\" value of this div is <span>?</span></p>\n"},"desc":"Returns value at named data store for the element, as set by <code>jQuery.data(element, name, value)</code>, or the full data store for the element.","longdesc":"\n <p><strong>Note:</strong> This is a low-level method; a more convenient <code><a href=\"/data/\">.data()</a></code> is also available.</p>\n <p>The <code>jQuery.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 free from memory leaks. jQuery ensures that the data is removed when DOM elements are removed via jQuery methods, and when the user leaves the page. We can set several distinct values for a single element and retrieve them later:</p>\n <pre><code>\njQuery.data( document.body, \"foo\", 52 );\njQuery.data( document.body, \"bar\", \"test\" );\n </code></pre>\n <p><em>Note:</em> this method currently does not provide cross-platform support for setting data on XML documents, as Internet Explorer does not allow data to be attached via expando properties.</p>\n "}]} |