In the following jQuery snippet, why does the foo2 object have no elements? My alert shows me that foo1 does contain DOM elements. Shouldn't find("*") then just return what is effectively a clone of foo1?
var foo1 = $("<div id='content'>HAGGIS</div&g开发者_运维问答t;");
var foo2 = foo1.find("*");
alert("foo1("+foo1.length+"): "+$('<div>').append(foo1.clone()).remove().html()
+ "\n\n" +
"foo2("+foo2.length+"): "+$('<div>').append(foo2.clone()).remove().html());
foo2
has no elements because the selector is for all children of foo1
, which has no child elements, just a text node.
From the jQuery documentation about .find()
Description: Get the descendants of each element in the current set of matched elements, filtered by a selector.
foo1
has no descendants.
No, if I understand correctly your question, then I'd say that find()
will look for the children. That is why foo2.length
will give you 0. If you want the clone of foo1
then var foo2 = foo1.clone();
would be the right one.
精彩评论