开发者

jQuery reports no direct descendants of document

I'm using jQuery 1.6.1 at the moment. In Firebug, I've seen that this:

console.log($(document).find("*"))

does what I expect; it returns all the children of the document. But this:

console.log($(document).find("> *"))

does not. It returns an empty set! It seems to me that, if the document has descendants, then it 开发者_开发问答must have at least one direct descendant. jQuery apparently disagrees. Is this a bug, or a misunderstanding on my part? Note also that this:

console.log($("body").find("> *"))

does what I expect, it returns direct descendants of the body tag. Thanks in advance for any insight!


document has indeed (at least) one child, which is an HTMLHtmlElement (which inherits form HTMLElement), so yes, theoretically, jQuery(or Sizzle in this case) should return this one.

Is it a bug? It might be a design decision. But lets see what $(document).children() gives us:

alert($(document).children().length);

outputs 1.

It is also interesting that $(document).find("> html") returns an empty set, whereas $(document).find("html") returns the HTMLHtmlElement. But the following is true:

$(document).find("html").parent()[0] === document

At least it is an inconsistency in jQuery. .find('> *') should return the same elements as .children() IMO.

It may be a bug in Sizzle or jQuery (someone with enough time can have a look at Sizzle's source code [source] and find out where the problem could be).

On the other hand, one can say that the child selector only works on Element nodes, and document is not an Element node. From this point of view, there is no bug, just this inconsistency.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜