jQuery show()vs添加类 [英] jQuery show() vs adding Class

查看:102
本文介绍了jQuery show()vs添加类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其中哪些更有效(即更快):

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

$(elem).show();

$(elem).addClass(displayClass); // Where display class is "display: block;"

还是相同?

推荐答案

这取决于你之后,他们做不同的事情:

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

这是更快吗? .addClass() 下来,您可以在这里测试,只需花费更少的工作量 .show() does 。然而,它不会做特定方面的,所以它的灵活性较低上述原因。

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.

这篇关于jQuery show()vs添加类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆