JQuery class selector vs id selector

后端 未结 5 626
天涯浪人
天涯浪人 2020-12-16 17:31

I have 7 different buttons that all perform the same javascript function on click. should i use class selector or id selector.

$(\"input.printing\").on(\"c         


        
5条回答
  •  佛祖请我去吃肉
    2020-12-16 17:49

    Selecting just by Id is faster than selecting by class in general since jQuery under the hood uses getElementById which is faster in most browsers. Please refer to this article for more information as it shows many other ways to improve performance related to Jquery. However, please remember selector performance is not the only thing that you have to be concerned about. Attaching to many unnecessary event could also create performance issue. To mitigate around this problem you could use delegate please see this jQuery documentation for more information regarding delegate.

提交回复
热议问题