jQuery show() vs adding Class

前端 未结 3 1647
小蘑菇
小蘑菇 2020-12-17 19:38

Which of these is more efficient (i.e. faster):

$(elem).show();

or

$(elem).addClass(displayClass); // Where display class i         


        
3条回答
  •  情深已故
    2020-12-17 20:09

    It depends what you're after, they do different things:

    • $(elem).show(); - shows the element, restoring the display from before .hide() or restoring the default display for the element type
    • $(elem).addClass(displayClass); - adds a class, always with a certain display, not really restoring what was there - this is less flexible

    Which is faster? .addClass() hands down, you can test it yourself here, it simply does a lot less work than .show() does. However, it doesn't do as much feature-wise, so it's less flexible for the reasons above.

提交回复
热议问题