我知道$("#id")更快,因为它映射到原生javascript方法。$("body")也是如此吗?
发布于 2010-12-10 04:06:51
不,它不使用Sizzle,有一个特殊的$("body")快捷方式,you can see the code here
// 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节点一样)都有自己的上下文。
发布于 2010-12-10 04:07:01
这是直接从source (code)获取的
if ( selector === "body" && !context && document.body ) {
this.context = document;
this[0] = document.body;
this.selector = "body";
this.length = 1;
return this;
}除body之外的其他标记的
如果你深入挖掘一下,就会发现如果没有给定上下文,他们就会使用getElementsByTagName。与使用Sizzle引擎相比,这将给性能带来很好的提升。
// 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, $(...))
}https://stackoverflow.com/questions/4402597
复制相似问题