Changing variable by html button

前端 未结 8 1220
野的像风
野的像风 2021-01-18 00:23

I\'m learning javascript and I decided to create simple Rock, Paper, Scissors game. I want to make it controllable by buttons. So I made this in html:



        
相关标签:
8条回答
  • 2021-01-18 01:27

    The other answers are fixing some issues. There is also a problem with the way you're calling the functions as you're passing rock as a variable, you need to use a string:

    <button onClick="user('rock')">Rock</button>
    

    Unless you're declaring the variables somewhere but it's not shown in your code.

    0 讨论(0)
  • 2021-01-18 01:28

    Maybe try this.. cleaner markup.. uses jQuery

    <div id="game">
        <button class="user" data-name="rock">Rock</button>
        <button class="user" data-name="paper">Paper</button>
        <button class="user" data-name="scissors">Scissors</button>
        <div id="result"></div>
        <br>
        <br>
        <button id="test">DEBUG</button>
    </div>
    
    
    $(document).ready(function() {
        var user = "none";
        $(".user").click(function() {
           user = $(this).attr("data-name");
        });
    
        $("#test").click(function() {
           alert(user);
        });
    });
    

    http://jsfiddle.net/rQDbe/

    0 讨论(0)
提交回复
热议问题