簡體   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