我想问我正在运行如下代码:
document.addEventListener("DOMContentLoaded", () => {
document.getElementById("named").onsubmit = () => {
var name = document.getElementById("name").value;
localStorage.setItem("name", name);
select_create();
return false;
}
document.getElementById("channel_created").onsubmit = () => {
success("Success!");
return false;}});
此代码可确保在成功加载DOM后应应运行代码。 但是,上面的代码块(document.getElementById(“ named”))可以正常工作,但是当我打开浏览器的控制台时,该控制台说:
TypeError:document.getElementById(“ channel_created”)为空
HTML代码具有ID为“ channel_created”的形式。如果我在控制台中粘贴相同的第二个代码块,它将起作用。我错过了一些步骤吗?
更新:我认为我必须显示HTML代码,相关代码如下:
<div id="create">
<form class="no-style" id="channel_created">
<h1>Create a Channel to Talk (be specific)</h1>
<div><input type="text" id="channel_name" name="channel_name" required placeholder="Please enter a channel name"><input type="submit" name="submit" value="Create a Channel Now!"></div>
</form>
</div>