从jQuery集合中删除注释和文本节点

whe*_*hys 4 javascript jquery comments textnode

$html = $("<!-- comment --> <p>text</p>");
Run Code Online (Sandbox Code Playgroud)

像这样创建一个jQuery集合

$( [the comment], [text node], p )
Run Code Online (Sandbox Code Playgroud)

我怎样才能访问该段落?.find("p")返回一个空集合

而且,对于额外的积分,

$html = $("<p>text</p>");
Run Code Online (Sandbox Code Playgroud)

像这样创建一个jQuery集合

$( p )
Run Code Online (Sandbox Code Playgroud)

是否有一种失败的安全方式来获取p,只有p,无论评论是否存在都有效?

lon*_*day 7

最简单的方法是使用filter通用选择器*,它匹配所有元素.

$html = $("<!-- comment --> <p>text</p>").filter('*');
Run Code Online (Sandbox Code Playgroud)