Does jQuery do any kind of caching of “selectors”?

后端 未结 14 2164
挽巷
挽巷 2020-11-27 18:14

For example, will the first piece of code perform a full search twice, or is it smart enough to cache results if no DOM changes have occurred?

if ($(\"#navba         


        
14条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 18:55

    It's not so much a matter of 'does it?', but 'can it?', and no, it can't - you may have added additional matching elements to the DOM since the query was last run. This would make the cached result stale, and jQuery would have no (sensible) way to tell other than running the query again.

    For example:

    $('#someid .someclass').show();
    $('#someid').append('
    New!
    '); $('#someid .someclass').hide();

    In this example, the newly added element would not be hidden if there was any caching of the query - it would hide only the elements that were revealed earlier.

提交回复
热议问题