JQuery class selector vs id selector

后端 未结 5 623
天涯浪人
天涯浪人 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:52

    As most have stated, the selector you use can have an effect on performance, but it is often negligible, and more a matter of style and readability. Bottom line, jQuery will create a unique handler for every element that is selected, either individually by id or using a multi-select method like a class or attribute selector. Personally, I like your first example, as it will cover future additions to your view without need to remember to add another handler.

提交回复
热议问题