提高 jQuery 选择器性能的好方法? [英] Good ways to improve jQuery selector performance?

查看:30
本文介绍了提高 jQuery 选择器性能的好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找任何可以提高 jQuery 调用的选择器性能的方法.具体是这样的:

I'm looking for any way that I can improve the selector performance of a jQuery call. Specifically things like this:

$("div.myclass")$(".myclass")

我认为可能是这样,但我不知道 jQuery 是否足够聪明,可以首先按标签名称等限制搜索.有人对如何制定 jQuery 选择器字符串以获得最佳性能有任何想法吗?

I would think it might be, but I don't know if jQuery is smart enough to limit the search by tag name first, etc. Anyone have any ideas for how to formulate a jQuery selector string for best performance?

推荐答案

毫无疑问,先按标签名过滤比按类名过滤要快得多.

There is no doubt that filtering by tag name first is much faster than filtering by classname.

直到所有浏览器都本地实现 getElementsByClassName 之前,情况将一直如此,就像 getElementsByTagName 的情况一样.

This will be the case until all browsers implement getElementsByClassName natively, as is the case with getElementsByTagName.

这篇关于提高 jQuery 选择器性能的好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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