javascript - HTML - 如何通过按下按钮使文本消失并显示不同的文本?

标签 javascript jquery html

所以我试图在这个网站上制作这个小小的“选择游戏”。

http://puu.sh/ncAr8/be095ee6e2.png

我已经做到了这一点,但是...当您单击“确定”时,它会将您带到另一个页面。我不想那样做。

相反,当您单击“确定”时,我希望文本和按钮消失并显示不同的文本/按钮。

这是我的代码。请原谅我糟糕的 HTML,我没那么优秀。

<!DOCTYPE html>
<html>
<head>
<meta name="theme-color" content="#18191b">
<link rel="icon" sizes="192x192" href="assets/trans.gif">
<title>...</title>
</head>


<div class="audio">
<audio autoplay="true" src="assets/Virtual Campfire with Crackling Fire Sounds (HD).mp3"/>
</div>


<div id="test">
<center><h1 class="center2">Do you want to join the campfire?<br> There is one person there... looking sad.</h1></center>
</div>
<center><a href="join1.html" class="center3">Sure.</a></center>


<div id="test">
<center><img class="center" src="assets/trans.gif"/></center>
</div>

<style>
    img.center {
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-position: center;
    display:block;
    position: relative;
    top: 200px;
    }
    h1.center2 {
    margin: auto;
    width: 60%;
    padding: 10px;
    color: white;
    font-family: "Myriad Pro";
    text-align: center;
    top: 70px;
    position: relative;
}
a.center3 {
    margin: auto;
    width: 60%;
    padding: 10px;
    color: white;
    font-family: "Calibri Light";
    text-align: center;
    top: 80px;
    position: relative;
    font-size: 25px;
    color: #EEB300;
}
#test h1 {
    margin-top: 25px;
    font-size: 45px;
    text-align: center;

    -webkit-animation: fadein 2s; /* Safari, Chrome and Opera > 12.1 */
       -moz-animation: fadein 2s; /* Firefox < 16 */
        -ms-animation: fadein 2s; /* Internet Explorer */
         -o-animation: fadein 2s; /* Opera < 12.1 */
            animation: fadein 2s;
}

@keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Firefox < 16 */
@-moz-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Safari, Chrome and Opera > 12.1 */
@-webkit-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Internet Explorer */
@-ms-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Opera < 12.1 */
@-o-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}
</style>

<body style="background-color:#18191b">
</body>

</html>

请帮助我,我不太擅长 HTML,也希望得到一些建议。谢谢!

最佳答案

我已经根据您的代码创建了一个工作示例。 您可以在 http://codepen.io/imanik/pen/bEyVpj

之前在此处查看

$("#confirm-btn").on("click", function() {
  $(this).hide();
  $("#test").hide();
  $("#another").fadeIn("slow");
  return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>

<head>
  <meta name="theme-color" content="#18191b">
  <link rel="icon" sizes="192x192" href="assets/trans.gif">
  <title>...</title>
</head>


<div class="audio">
  <audio autoplay="true" src="assets/Virtual Campfire with Crackling Fire Sounds (HD).mp3" />
</div>


<div id="test">
  <center>
    <h1 class="center2">Do you want to join the campfire?<br> There is one person there... looking sad.</h1>
  </center>
</div>
<div id="another" style="display:none;">
  <center>
    <h1 class="center2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur sit doloribus officiis praesentium assumenda, quod eaque illum voluptatem voluptatum distinctio.</h1>
  </center>
</div>
<center><a href="join1.html" class="center3" id="confirm-btn">Sure.</a>
</center>


<div id="test">
  <center>
    <img class="center" src="assets/trans.gif" />
  </center>
</div>

<style>
  img.center {
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-position: center;
    display: block;
    position: relative;
    top: 200px;
  }
  h1.center2 {
    margin: auto;
    width: 60%;
    padding: 10px;
    color: white;
    font-family: "Myriad Pro";
    text-align: center;
    top: 70px;
    position: relative;
  }
  a.center3 {
    margin: auto;
    width: 60%;
    padding: 10px;
    color: white;
    font-family: "Calibri Light";
    text-align: center;
    top: 80px;
    position: relative;
    font-size: 25px;
    color: #EEB300;
  }
  #test h1 {
    margin-top: 25px;
    font-size: 45px;
    text-align: center;
    -webkit-animation: fadein 2s;
    /* Safari, Chrome and Opera > 12.1 */
    -moz-animation: fadein 2s;
    /* Firefox < 16 */
    -ms-animation: fadein 2s;
    /* Internet Explorer */
    -o-animation: fadein 2s;
    /* Opera < 12.1 */
    animation: fadein 2s;
  }
  @keyframes fadein {
    from {
      opacity: 0;
    }
    to {
      opacity: 1;
    }
  }
  /* Firefox < 16 */
  @-moz-keyframes fadein {
    from {
      opacity: 0;
    }
    to {
      opacity: 1;
    }
  }
  /* Safari, Chrome and Opera > 12.1 */
  @-webkit-keyframes fadein {
    from {
      opacity: 0;
    }
    to {
      opacity: 1;
    }
  }
  /* Internet Explorer */
  @-ms-keyframes fadein {
    from {
      opacity: 0;
    }
    to {
      opacity: 1;
    }
  }
  /* Opera < 12.1 */
  @-o-keyframes fadein {
    from {
      opacity: 0;
    }
    to {
      opacity: 1;
    }
  }
</style>

<body style="background-color:#18191b">
</body>

</html>

其工作方式是将 CSS 显示属性设置为 none,以便将隐藏的内容隐藏起来。

然后使用 Jquery 定位按钮并监听单击事件。当发生这种情况时,只需隐藏您想要隐藏的所有元素并显示其可见的元素即可。

关于javascript - HTML - 如何通过按下按钮使文本消失并显示不同的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35487485/

相关文章:

javascript - 冒泡排序不交换 Javascript 中的数组元素

javascript - 停止多个动画直到另一个动画完成

javascript - 在jquery中获取数组中第一个元素之前的逗号

javascript - 使 svg 元素位于顶部

javascript - 如何使轮播占据整个页面宽度

html - 如何对非同级元素使用 nth-of-type

html - 使 Google 自定义搜索在新标签页上显示结果

javascript - 使用web3部署智能合约不支持同步请求

javascript - SVG 多边形形状与图像

javascript - 从网络服务获取图像