<分区>
标签 javascript jquery html css
这是我在 CodePen 上制作的笔的链接:http://codepen.io/PartTimeCoder/pen/KzQQvM?editors=0010
我的 Javascript,问题一定出在 $("img").click() 函数底部的 if 命令中:
$("img").click(function() {
var user;
user = this.id;
var computer = Math.random();
if (computer <= 0.33) {
computer = "Rock"
} else if (computer <= 0.67) {
computer = "Scissors"
} else {
computer = "Paper"
}
$(".cpu").html("The computer chose - " + computer);
$(".you").html("You chose - " + user);
$(".result").html("");
if (computer == user) {
$(".result").html("It's a tie!");
}
if (user == "paper") {
if (computer == "rock") {
$(".result").html("You win!");
} else {
$(".result").html("You lose!");
}
}
if (user == "rock") {
if (computer == "scissors") {
$(".result").html("You win!");
} else {
$(".result").html("You lose!");
}
}
if (user == "scissors") {
if (computer == "paper") {
$(".result").html("You win!");
} else {
$(".result").html("You lose!");
}
}
});
最佳答案
您的 ID 是大写的,但您试图匹配小写。更改代码或标记。
例如,rock 的 id 是 Rock
,但您的逻辑试图匹配 user == 'rock'
。如果您将 id 更改为 rock
,您的代码应该可以正常工作。您需要为每个 id 重复此操作,即石头、布和剪刀。
关于javascript - 我在 jQuery 中的剪刀石头布程序只在平局时返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36561199/