我正在運行w3schools的代碼來動態添加按鈕到頁面。這是代碼。document.body.appendChild()不能與<form>
<!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>
標籤這個充滿活力的另外不起作用。我可以在不需要移除<form>
標籤的情況下使JavaScript適應動態按鈕的哪些變化?
到底是什麼問題?我不明白你的陳述「但是當我有一個標籤時,這種動態添加不起作用」。 –
不確定你的意思是「當我有標籤時不工作」。你應該努力工作。選中此項:https://jsfiddle.net/mspinks/tcgmf8r7/2/ –
您在嘗試使用哪種瀏覽器? – putvande