javascript - x秒后显示一个div,y秒后显示另一个div

标签 javascript html

我对此很陌生,但我试图在 1 秒后显示 div1,并在 5 秒后显示 div2。目前,只有 div2 在 1 秒后显示。请帮我修复 javascript。

我还想知道如何在显示 div2 后隐藏 div1。

我的代码:

<div id="div1" style="visibility: hidden">Reveal Div 1 after 1 second</div>
<div id="div2" style="visibility: hidden">Reveal Div 2 after 5 seconds</div>

<script type="text/javascript">
function showIt() {
  document.getElementById("div1").style.visibility = "visible";
}
setTimeout("showIt()", 1000); // after 1 sec

function showIt() {
  document.getElementById("div2").style.visibility = "visible";
}
setTimeout("showIt()", 5000); // after 5 secs
</script>

最佳答案

不能有 2 个同名的函数。将第二部分更改为

function showIt2() {
  document.getElementById("div2").style.visibility = "visible";
}
setTimeout("showIt2()", 5000); // after 5 secs

演示:http://jsfiddle.net/W9YU5/

关于javascript - x秒后显示一个div,y秒后显示另一个div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18808383/

相关文章:

javascript - js滚动条不可见

javascript - 推送新 URL 时,React 组件渲染会被多次调用

html - 在一个大块中显示图像缩略图的最佳方式是什么?

html - 带有 CSS 的 XSLT 不适用

javascript - 当密码或用户名错误时在登录中添加 toast

javascript - 检查拖放内容是否为图像

javascript - 如何从数组中提取两列

java - 保留以前的表单数据

javascript - 如何在javascript中对两个对象值求和

时间轴帖子的 HTML 元素