The
.data() 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.$( "div" ).data( "foo", 52 );$( "div" ).data( "bar", { myType: "test", count: 40 } );$( "div" ).data( { baz: [ 1, 2, 3 ] } );$( "div" ).data( "foo" ); // 52$( "div" ).data(); // { foo: 52,// bar:{ myType: "test", count: 40 },// baz: [ 1, 2, 3 ] }Example:
<body><div>The values stored were<span></span>and<span></span></div><script>$( "div" ).data( "test", { first: 16, last: "pizza!" } );$( "span:first" ).text( $( "div" ).data( "test" ).first );$( "span:last" ).text( $( "div" ).data( "test" ).last );</script></body>Output:The values stored were 16 and pizza!
No comments:
Post a Comment