如何为不同的功能分配相同的yes no按钮

时间:2015-02-14 14:00:19

标签: javascript function button

我有两个是没有按钮,我希望在程序的不同阶段使用不同的功能。 因为它们与一个功能相关联

  <input type="button" value="Yes" class="yes" onclick="*choice1(1)*;" />
  <input type="button" value="No" class="no" onclick="*choice1(0)*;" />

如何使它们对其他功能也有用,所以我不需要更多冗余按钮。

我试图创建一个互动故事。在不同阶段,可以选择两种选择。我正在使用if else方法,这使得我很难理解为函数指定一个按钮 - 因为每个函数每次都使用两个选项。

这是代码(非常感谢:):

       <script type="text/javascript">
        var photo = "photo";
        var edgar = "edgar"
        var x;

        function choice1(x){            
        if (x == 1)
        {
       document.getElementById("photo").src = "images/dragon1.jpg";
       document.getElementById("stage1").innerHTML = "Once upon a time  
       there was a dragon";
       setTimeout("choice2()",5*1000);

      }   
       else if (x == 0)
        { 
       document.getElementById("photo").src = "images/sea.jpg";
       document.getElementById("stage1").innerHTML = "Would you like to  
       listen to some music?";
       document.getElementById("edgar").play();
        }
    } 
     function choice2(x){

      document.getElementById("photo").src = "images/dragon9.jpg";
      document.getElementById("stage1").innerHTML = "Was he a friendly 
      dragon?";

     if (x == 1)
      {document.getElementById("photo").src = "images/dragon2.jpg";
       document.getElementById("stage1").innerHTML = "He had many 
     friends and he liked to party";
    }
    else if (x == 0)
    { document.getElementByID ("photo").src = "images/dragon3.jpg";
      document.getElementById("stage1").innerHTML = "He spent his days 
    thinking about the wrongs of the world";
    }   
    }

    </script>
    </head>
    <body>
    <table align="center">
        <tr>
            <td colspan="2" align="center" width="800" height="300"><img 
        id="photo" width="800" height="300"></td>

        </tr>
        <tr>
            <td colspan="2" align="center" width="800" height="100"><h1 
    id="stage1">Would you like to read a story?</h1></td>

    </tr>
    <tr>
        <td align="center" width="400" height="200"><input type="button" 
    value="Yes" class="yes" onclick="choice1(1);" /></td>
        <td align="center" width="400" height="200"><input type="button" 
    value="No" class="no" onclick="choice1(0);" /></td>
    </tr> 
    </table>
    </body>
    </html>

0 个答案:

没有答案