表单选择器无法处理动态标记

Pav*_*min 5 javascript jquery

当我执行以下代码时,它的行为与我期望的一样(记录DIV元素的内容):

var html = '<form action="/" method="get" name="myform"><div>123</div></form>';
console.log($('div', html));
Run Code Online (Sandbox Code Playgroud)

我无法理解的是为什么以下代码不起作用:

var html = '<form action="/" method="get" name="myform"><div>123</div></form>';
console.log($('form', html));
Run Code Online (Sandbox Code Playgroud)

它们看起来是一样的,为什么DIV选择FORM器不工作时选择器工作?

new*_*Bee 0

var html = '<form action="/" method="get" name="myform"><div>123</div></form>';
console.log($(html).html());
Run Code Online (Sandbox Code Playgroud)

由于表单是 的根元素html$(html)因此已经返回您所需的 JQuery 对象。因此,如果您尝试选择一个表单标签,Jquery 会尝试在表单中查找另一个表单标签,当然,该标签不存在。