[英]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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.