javascript - 如何使用 setTimeout 和 setInterval 显示 HTML 内容?
问题描述
如何将图像显示几秒钟,然后将其隐藏并显示其余的 HTML 部分。
最初,我想显示图像 2 秒并隐藏 DIV 直到时间。2 秒后隐藏图像并仅显示 div。
我尝试使用 js
setTimeout(() => document.getElementById('Image').style.display = 'block', 2000);
setInterval(() => document.getElementById('Item').style.display = 'none', 2000);
<div id="Image">
<img src="../img-correct.png" alt="image">
</div>
<div class="item" id="Item">
<h3>Time:</h3>
</div>
我首先得到 div,然后在 2 秒后得到图像。
解决方案
Set image display to: 'block' and item display to: 'none' then change both after 2000ms.
const item = document.getElementById('Item');
item.style.display = 'none';
const image = document.getElementById('Image');
image.style.display = 'block';
setTimeout(() => {
item.style.display = 'block';
image.style.display = 'none';
}
,2000);
推荐阅读
- elasticsearch - 具有多个条件的弹性搜索查询
- html - 如何获取角度表单输入文本值?
- salesforce - Salesforce Lightening 跨域问题
- abstract-syntax-tree - 如何遍历由 esprima 生成的 AST?
- azure - 从不同组织的管道下载工件(在 yaml 管道中)
- go - 使用 ssh 包 Golang 时出现 SSH_FX_PERMISSION_DENIED,但使用 filezilla 进行复制工作正常
- flutter - Streambuilder 在收到流时如何工作?
- flutter - 在列表中,点击的项目 ID 未正确进入 onTap 函数
- python - RequestsLibrary - 如何在同一个 POST 请求中发送数据和文件
- c# - 使用 CefSharp SendKeyEvent 时,我无法键入文档