$("#id")
ネイティブのjavascriptメソッドにマッピングされるため、より高速であると理解しています。同じことが当てはまり$("body")
ますか?
2 に答える
10
いいえ、Sizzle は使用しません。特別なショートカットが用意$("body")
されています。コードは次のとおりです。
// 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)
。 as の結果のコンテキストは$("body")
isdocument
であり、 as は$(document.body)
(他の DOM ノードと同様に) それ自体のコンテキストを持ちます。
于 2010-12-09T20:06:51.217 に答える
6
これはソース(コード)から直接です:
if ( selector === "body" && !context && document.body ) {
this.context = document;
this[0] = document.body;
this.selector = "body";
this.length = 1;
return this;
}
本文以外のタグの場合
もう少し深く掘り下げると、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, $(...))
}
于 2010-12-09T20:07:00.840 に答える