I have a question about .data (). In a stackoverflow response, jquery stores data in a cache object, where the index is cached by the suffix of the dom element. For example, consider an element as:
- #result ID,
- in the third jQuery cache index
- with key: "carModel" && & value: "Audi"
Now I read .data () will save the key-value with the hash value of the result identifier and cache index. Now my question is, how is jquery hash an arbitrary element in dom?
For example, consider the following html:
<div id="begin"> <p id="hello">Hello World</p> <p id="another">another</p> <div> <p> test p </p> </div> <p id="last">last</p> <div>
Code for assigning a value to each of them:
var i=0; $("p").each( function() { $(this).data("value",i); ++i; });
Finally, return the value of the largest p element:
alert( $("div div p").data("value") );
I want to know how this works? :-) I mean, how did jquery hash elements in such a way that they can be obtained without a specific identifier (such as an element identifier) for them?
Thanks for the help...
source share