Is there an advantage in how I store my data using jQuery?

后端 未结 3 1340
终归单人心
终归单人心 2021-01-27 01:36

I know understand more about how jQuery stores data.

Is there any advantage to doing one or the other of these:

$(\'#editCity\').data(\'href\', \"xx\");         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-27 02:19

    The most interesting point about the data function is that you can add any kind of object, for example your modal. And jQuery, as stated in the documentation, take care of avoiding memory leaks when the DOM changes :

    The jQuery.data() 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.

    For strings, memory leaks aren't possible but the main difference is that the first solution is cleaner (more coherent if you might store other data than strings in other parts of your application), clearer (the intent is evident), and doesn't force CSS calculation (DOM isn't changed).

提交回复
热议问题