Javascript:图片按钮

时间:2016-04-27 12:30:05

标签: javascript html

我有一个javascript测验,我想在用户点击按钮时显示图片。例如:当他们点击正确的答案时,会出现绿色的thubs。这是我目前的代码:

<!DOCTYPE html>
<html>

<head>
  <title>JavaScript Task 1</title>
  <meta charset="UTF-8">
  <style>
    ul {
      list-style-type: none;
      margin: 0;
      padding: 0;
      overflow: hidden;
      background-color: #333;
      border-style: solid;
    }
    li {
      float: left;
    }
    li a {
      display: block;
      color: black;
      text-align: center;
      padding: 14px 55px;
      text-decoration: none;
    }
    li a:hover {
      background-color: #111;
      border-style: solid;
    }
    .tasks {
      color: black;
      font-size: 250%;
      text-align: center;
    }
    .t&f {
      color: white;
      font-size: 250%;
      text-align: center;
      border-style: solid;
    }
  </style>
</head>

<body>
  <ul>
    <li>
      <a href="index_page.html">Home</a>
    </li>
    <li>
      <a href="task1.html">Task 1</a>
    </li>
    <li>
      <a href="task2.html">Task 2</a>
    </li>
    <li>
      <a href="task3.html">Task 3</a>
    </li>
    <li>
      <a href="task4.html">Task 4</a>
    </li>
    <li>
      <a href="task5.html">Task 5</a>
    </li>
    <li>
      <a href="task6.html">Task 6</a>
    </li>
    <li>
      <a href="task7.html">Task 7</a>
    </li>
  </ul>
  <script src="script-1.js" type="text/javascript">
  </script>
  <script>
  </script>
  <div class='tasks'>
    <div class='container'>
      <h1>Task 1</h1>
    </div>
  </div>
  <div class='tasks'>
    <div class='container'>
      <h3>True or False Quiz</h3>
      <h5>Question One</h5>
      <h6>1) Michael Jackson’s Thriller is the greatest-selling album of
            all time.</h6>
      <button onclick="document.bgColor='Green'; alert('Correct! Good job :)')">True</button>
      <button onclick="document.bgColor='Red'; alert('Wrong! Unlucky. Try again :(')">False</button>
    </div>
  </div>
  <div class='tasks'>
    <div class='container'>
      <h5>Question Two</h5>
      <h6>2) The modern Caesar salad is derived from a recipe dating back
            to ancient Rome.</h6>
      <button onclick="document.bgColor='Red';alert('Wrong! Unlucky. Try again :(')">True</button>
      <button onclick="document.bgColor='#00cc00'; alert('Correct! Good job :)')">False</button>
    </div>
  </div>
  <div class='tasks'>
    <div class='container'>
      <h5>Question Three</h5>
      <h6>3) Ozone is "good" in the upper atmosphere but "bad" in the
            lower.</h6>
      <button onclick="document.bgColor='Green'; alert('Correct! Good job :)')">True</button>
      <button onclick="document.bgColor='#ff3300'; alert('Wrong! Unlucky. Try again :(')">
        False</button>
    </div>
  </div>
</body>

</html>

非常感谢

詹姆斯

2 个答案:

答案 0 :(得分:0)

您可以尝试使用javascript

<button id="mybutton" onclick="showButton()">Show Image </button>

function showButton(){
  image = document.getElementById('nameOfImageHere');
  image.style.display = "block";
}

答案 1 :(得分:0)

我们走了:

1方法: 您可以使用jQuery Hide(),Show()函数:

          <script>
            $(document).ready(function(){
                $("#buttonIdShow").click(function(){
                    $("#Image").hide();
                });
                $("#buttonIdHide").click(function(){
                    $("#Image").show();
                });
            });
            </script>



        <img id="Image"/>               <!-Here is your image-->
    <button type="button" id="buttonIdShow">Show</button>  <!--Show button-->

    <button type="button" id="buttonIdHide">Hide</button> <!--Hide button-->

2方法:jQuery Animate CSS显示:

<script>
$("#yourButton1Id").click(function(){
  $("#ImageId").css("display","block");
});

$("#yourButton2Id").click(function(){
  $("#ImageId").css("display","none");
});
</script>

希望它有所帮助;)