如何在纯 Javascript 中删除和隐藏 HTML 元素?

发布于 2024-12-05 01:40:17 字数 705 浏览 1 评论 0原文

我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜巴黎 2024-12-12 01:40:17

您需要将元素引用传递给 removeChild,而不是字符串

body.removeChild(document.getElementById('verifying'));

:也可以隐藏它:

document.getElementById('verifying').style.display = "none";

You need to pass an element reference to removeChild, not a string:

body.removeChild(document.getElementById('verifying'));

You could also just hide it:

document.getElementById('verifying').style.display = "none";
玻璃人 2024-12-12 01:40:17

你的removeChild需要获取一个元素,而不是一个字符串

var body = document.body;
var signup = document.getElementById("content-container");

setTimeout(function(){
    body.removeChild(document.getElementById('verifying'));
    signup.style.display = "block";
}, 5000);

your removeChild needs to get an element, not a string

var body = document.body;
var signup = document.getElementById("content-container");

setTimeout(function(){
    body.removeChild(document.getElementById('verifying'));
    signup.style.display = "block";
}, 5000);
只是我以为 2024-12-12 01:40:17

要删除您可以使用(如上所述)removeChild:

var x = document.getElementById('elementid');
x.parentNode.removeChild(x);

并隐藏一个元素:

var x = document.getElementById('elementid');
x.style.display="none";

编辑:

哦,如果您希望它隐藏但不“脱离流程”,请使用以下命令:

var x = document.getElementById('elementid');
x.style.visibility="hidden";

to remove you can use (as stated) removeChild:

var x = document.getElementById('elementid');
x.parentNode.removeChild(x);

And to hide an element:

var x = document.getElementById('elementid');
x.style.display="none";

EDIT:

oh and if you want it hidden but not taken "out of flow", use this:

var x = document.getElementById('elementid');
x.style.visibility="hidden";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文