一个简单的例子。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Title</title>
<script src="jquery-2.1.4.js"></script>
</head>
<body>
<form id="f1">
</form>
<script>
$('#f1').html(
'<div id="outer">' +
'<form id="f2">' +
'<div id="inner"></div>' +
'</form>' +
'</div>'
);
</script>
</body>
</html>
如果我们在浏览器中打开此页面,我们将看不到 #f2
表单。
为什么会被删除?
我知道嵌套表单根据标准无效,但在我正在研究的项目中,在表单内插入表单有时可以工作,但我无法弄清楚其中的区别(在这两种情况下,表单都是通过 .html()
插入的) .
我也尝试过 innerHTML
,但结果是一样的。
最佳答案
您遇到了未定义的行为。
Note: It's strictly forbidden to nest a form inside another form. Nesting can cause forms to behave in an unpredictable manner based on the browser that is being used.
关于javascript - 为什么没有插入<form>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48560306/