何时使用jQuery的.find() [英] When to use jQuery's .find()

查看:53
本文介绍了何时使用jQuery的.find()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我什么时候使用jQuery的 .find() ?

When would I use jQuery's .find()?

例如,

$('tr').find('.someClass');等效于$('tr .someClass')

那么,什么时候可以成为在直接选择器上使用.find()的好例子?

So, when would be a good example of when you would use .find() over a straight selector?

还有,哪个表现更好?选择器比.find()更快吗?

Also, which is more performant? Is a selector quicker than .find()?

推荐答案

答案是可能的话.

与拥有子项/多项/CSS/上下文选择器相比,它总是性能更高,并且是执行速度最快的遍历机制.

It is always more performant than having children / multi-item / CSS / context selectors, and is the fastest-performing traversing mechanism.

jsPerf来显示我的意思.

您甚至可以考虑不使用它的唯一时间是,如果您只想选择直接子项,而这些子项恰好与您不想选择的子项具有相同的等级.这是.children()的工作,但很少见.

The only time you may even consider not using it is if you only want to select items that are direct children, and those children happen to have the same class as their children that you don't want to select. This is a job for .children(), but is a very rare case.

这篇关于何时使用jQuery的.find()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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