Using jQuery's datastore vs. expando properties

后端 未结 4 1774
我在风中等你
我在风中等你 2021-01-31 20:22

I\'m developing code using jQuery and need to store data associated with certain DOM elements. There are a bunch of other questions about how to store arbitrary data w

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-31 21:09

    If you are authoring a plugin you should use $.data. If you need to store the attribute often and rarely need to query the DOM for it then use $.data.

    Update 5 years later: jQuery does not query the DOM based on expando properties set, and hasn't done so for a while. So use $.data. There's no reason to pollute the DOM when there is no pragmatic use to do so.

提交回复
热议问题