函数不是't创建输入字段

Function isn't creating an input field?

本文关键字:创建 输入 字段 函数      更新时间:2023-09-26

我正在尝试创建一个函数,该函数使用onclick事件来显示输入字段。这是我的密码。

<!DOCTYPE html>
<html>
<body>
<button onclick="createMessage()">New Message</button>
<script>
function createMessage() {
    var input = document.createElement("input");
    input.type = "text";
    input.className = "message_input"; 
    container.appendChild(input);
};
</script>
</body>
</html>

我做错了什么?

如果这是页面的完整来源,则没有名为"container"的元素。在最简单的情况下,您可以将新的输入字段添加到文档本身。

更换

container.appendChild(input);

带有

document.documentElement.appendChild(input);