如何在纯 Javascript 中删除和隐藏 HTML 元素?
我有这个 HTML:
<body>
<div id="content-container" style="display:none">
<div>John</div>
</div>
<div id="verifying">
<div id="message">Verified</div>
</div>
</body>
和这个 Javascript:
var body = document.body;
var signup = document.getElementById("content-container");
setTimeout(function(){
body.removeChild('verifying');
signup.style.display = "block";
}, 5000);
我正在尝试删除
并显示 5 秒后,但由于某种原因它不起作用。知道为什么吗?我在页面加载后加载脚本,所以这不是问题。
I have this HTML:
<body>
<div id="content-container" style="display:none">
<div>John</div>
</div>
<div id="verifying">
<div id="message">Verified</div>
</div>
</body>
And this Javascript:
var body = document.body;
var signup = document.getElementById("content-container");
setTimeout(function(){
body.removeChild('verifying');
signup.style.display = "block";
}, 5000);
I am trying to remove <div id="verifying">
and show <div id="content-container">
after 5 seconds, but for some reason it is not working. Any idea why? I am loading the script after the page loads so that is not the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要将元素引用传递给
removeChild
,而不是字符串:也可以隐藏它:
You need to pass an element reference to
removeChild
, not a string:You could also just hide it:
你的removeChild需要获取一个元素,而不是一个字符串
your removeChild needs to get an element, not a string
要删除您可以使用(如上所述)removeChild:
并隐藏一个元素:
编辑:
哦,如果您希望它隐藏但不“脱离流程”,请使用以下命令:
to remove you can use (as stated) removeChild:
And to hide an element:
EDIT:
oh and if you want it hidden but not taken "out of flow", use this: