jQuery object: to cache or not to cache?

后端 未结 6 480
说谎
说谎 2020-11-27 07:43

I have some trouble that comes from my Javascript (JS) codes, since I sometimes need to access the same DOM elements more than once in the same function. Some reasoning is a

6条回答
  •  一生所求
    2020-11-27 08:32

    The second is superior. Most importantly, it is cleaner. In the future, if you want to change your selector, you only need to change it one place. Else you need to change it in N places.

    Secondly, it should perform better, although a user would only notice for particularly heavy dom, or if you were invoking that function a lot.

提交回复
热议问题