首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >$("body")是否使用Sizzle引擎?

$("body")是否使用Sizzle引擎?
EN

Stack Overflow用户
提问于 2010-12-10 04:03:46
回答 2查看 443关注 0票数 4

我知道$("#id")更快,因为它映射到原生javascript方法。$("body")也是如此吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-12-10 04:06:51

不,它不使用Sizzle,有一个特殊的$("body")快捷方式,you can see the code here

代码语言:javascript
复制
    // The body element only exists once, optimize finding it
    if ( selector === "body" && !context && document.body ) {
        this.context = document;
        this[0] = document.body;
        this.selector = "body";
        this.length = 1;
        return this;
    }

注意,这与$(document.body)不完全相同,因为$("body")的结果上下文是document,而as $(document.body) (与任何其他DOM节点一样)都有自己的上下文。

票数 10
EN

Stack Overflow用户

发布于 2010-12-10 04:07:01

这是直接从source (code)获取的

代码语言:javascript
复制
if ( selector === "body" && !context && document.body ) {
    this.context = document;
    this[0] = document.body;
    this.selector = "body";
    this.length = 1;
    return this;
}

除body之外的其他标记的

如果你深入挖掘一下,就会发现如果没有给定上下文,他们就会使用getElementsByTagName。与使用Sizzle引擎相比,这将给性能带来很好的提升。

代码语言:javascript
复制
// HANDLE: $("TAG")
} else if ( !context && !rnonword.test( selector ) ) {
    this.selector = selector;
    this.context = document;
    selector = document.getElementsByTagName( selector );
    return jQuery.merge( this, selector );

// HANDLE: $(expr, $(...))
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4402597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档