JQuery .data() how to work question

后端 未结 2 343
粉色の甜心
粉色の甜心 2021-01-24 17:26

I have a question about .data(). In a stackoverflow answer, jquery saves data in cache object where cache index is suffixed with a hash of the dom element. For example, consider

2条回答
  •  猫巷女王i
    2021-01-24 18:06

    I'm not sure you have understood what you have done with the JQUERY selectors.

    In your example HTML you nested 2 divs and then placed a single paragraph into the lowest level div. Then in your JQUERY you asked it to look for 2 divs and a paragraph. That's why you got the "correct" result. This actually has nothing to do with .data() as such, but the fact that there wasn't anything else for JQUERY to find, and therefore the result was always going to be "correct".

    However, I'm not certain you would get the "correct" result if you had a larger context with many nested divs that had no id values.

    I would say that it is good practice for you to assign an id value for all your

    and then pass the specific id whenever you use .data().

    In that way you can know what the outcome of your code will be as you create it.

提交回复
热议问题