繁体   English   中英

document.body.appendChild() 不工作<form>

[英]document.body.appendChild() not working with <form>

我正在运行 w3schools 的代码以动态地向页面添加按钮。 这是代码。

 <!DOCTYPE html> <html> <body> <p>Click the button to make a BUTTON element with text.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { var btn = document.createElement("BUTTON"); var t = document.createTextNode("CLICK ME"); btn.appendChild(t); document.body.appendChild(btn); } </script> </body> </html>

但是当我有一个<form>标签时,这个动态添加不起作用。 我可以对 JavaScript 进行哪些更改以适应动态按钮而无需删除<form>标记?

 function myFunction() { var btn = document.createElement("BUTTON"); var t = document.createTextNode("CLICK ME"); btn.appendChild(t); document.getElementById('theForm').appendChild(btn); } </script>
 <p>Click the button to make a BUTTON element with text.</p> <button onclick="myFunction()">Try it</button> <form id="theForm"> </form>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM