简体   繁体   中英

Using an image as a button that calls a function on-click Javascript

I am trying to program a game of rock paper scissors. When I click on my input, the function is not invoked. I am wondering if I am overlooking anything in my code:

function LFG(){
    var c=math.random();
    if (c<0.3333){
        cmove="r";
        document.getElementById("playahater").src("rightRockHand.jpg");
    }
    if (c>0.3333 && c<0.6666){
        cmove="p";
        document.getElementById("playahater").src("rightHandPaper.jpg");
    }
    if (c>0.6666){
        cmove="s";
        document.getElementById("playahater").src("rightHandScissors.jpg");
    }
    return cmove;
}

function rock(){
    LFG();
    document.getElementById("playa").src("leftRockHand.jpg");
    if (cmove == "r"){
        ties += ties;
        return ties;
    }
    if (cmove == "p"){
        losses += losses;
        return losses;
    }
    if (cmove == "s"){
        wins += wins;
        return wins;
    }
}

This code covers all of my functions, switching the second function for the logical substitute for paper and scissors. Here is where the function is invoked

<td><input onclick="scissor()" type="image" src="scissors.jpg" alt="scissors" id="scissors"/></td>
function rock(){
var cmove = LFG(); <-- here is your fix 
document.getElementById("playa").src("leftRockHand.jpg");
if (cmove == "r"){
    ties += ties;
    return ties;
}
if (cmove == "p"){
    losses += losses;
    return losses;
}
if (cmove == "s"){
    wins += wins;
    return wins;
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM