Performance of jQuery Selectors with ID

后端 未结 9 2031
有刺的猬
有刺的猬 2021-01-12 10:52

I know in jQuery if we use ID to select elements,it\'s so efficient.I have a question about this selectors:

please consider this 3 selectors:

         


        
9条回答
  •  天涯浪人
    2021-01-12 11:44

    A couple things:

    • More selectors = slower search. If you can get your desired result with fewer predicates, do so.
    • Getting an element by ID is quicker than getting by a class. getElementById is a core function of JavaScript that is heavily optimised because it is used so frequently. Leverage this when possible.
    • The space selector (' ') is much more costly than the child selector ('>'). If you can use the child selector, do so.

    These rules apply to CSS as they do JavaScript and jQuery.

提交回复
热议问题