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

时间:2016-02-18 16:47:43

标签: javascript jquery html

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

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

我做到了这一点但......当你点击Sure时,它会带你到另一页。我不想那样做。

相反,当您点击Sure时,我希望文本和按钮消失,并显示不同的文本/按钮。

这是我的代码。请原谅我糟糕的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非常苛刻,并且也喜欢一些建议。谢谢!

3 个答案:

答案 0 :(得分:2)

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

之前在此处查看

&#13;
&#13;
$("#confirm-btn").on("click", function() {
  $(this).hide();
  $("#test").hide();
  $("#another").fadeIn("slow");
  return false;
});
&#13;
<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>
&#13;
&#13;
&#13;

它的工作方式是您不希望在开头显示的内容将CSS显示属性设置为无,以便隐藏它。

然后使用Jquery定位按钮并监听click事件。当它发生时,只需隐藏你想要隐藏的所有元素,并显示哪些元素是可见的。

答案 1 :(得分:0)

你为什么不考虑jQuery?你必须做这样的步骤:

  1. 将两个内容分成两个不同的div,并给它们两个不同的ID。
  2. 使用属性display:none;
  3. 初始化另一个div
  4. 点击按钮,您可以为两个div添加属性。
  5. 代码:

    $().ready(function(){
        $("#button").click(function(){
            $("#div-to-hide").attr("display","none");
             $("#divtodisplay").attr("display","block");
         });
    });
    

    最初在<style>标记中,您必须设置

    #divtodisplaylater{
       display:none;
    }
    

    考虑jQuery用于不同的功能,JQuery网站上的功能细节

    http://api.jquery.com/attr/

答案 2 :(得分:0)

您不能为多个标记提供相同的ID,因此请改用类 <div id="test"><div class="test">

将CSS代码放在外部.css文件中,并在head元素中链接到它。

<head>
  <link rel="stylesheet" type="text/css" href="file_name.css"/>
  ...
</head>

<小时/> <小时/> 至于你的问题,试试:

<div id="will_disappear">
  <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="#" class="center3" onclick="document.getElementById('will_disappear').style.display = 'none';for (var i=0;i<document.getElementsByClassName('will_show').length;i++){document.getElementsByClassName('will_show')[i].style.display = 'block';}">Sure.</a></center>
</div>
<div class="will_show">
  Content...
</div>
.will_show {
  display: none;
}