I can’t quite understand why the find behaves here:
var data = $("<html><title>hello</title><body><form><ul><li>here</li></ul></form></body><ul><li></li></ul></html>");
alert(data.find("form").length);
alert(data.find("ul").length);
- It doesn’t find the form
- It doesn’t find the second UL
I’ve also put up a jsfiddle for this-
It doesn’t find the
formelement because it’s effectively the root element of the collection (it’s a direct child of thebodyelement). If you change it to.filter, it will find theform:It doesn’t find the second
ulelement because it’s outside of thebodyelement, which is invalid (jQuery puts the secondulin the root of the collection – usefilterand you’ll be able to find that one too).jQuery removes the
htmlelement and thebodyelement from the collection. If you log the contents of the collection it makes it a bit clearer what’s going on: