JQuery class selector vs id selector

后端 未结 5 627
天涯浪人
天涯浪人 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 18:06

    as we all know id selector is faster and better .. but in you case.. since you are selecting each id 8 times.. which compare to the class seletor is slower as you can see here

    class selector in your case is better, faster, and more readable...

    note: what if you have to add 20 more buttons in the near future (100 would be too much).....and you choose to go with id selector... phewwww!!! that is like, 20 more event handler...... ;) ;)

提交回复
热议问题