JavaScript 未在我的 HTML 中执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19214584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript not executing in my HTML
提问by LemenDrop
So I've begun learning HTML/CSS/JavaScript and came across this issue while trying to make an exceedingly simple version of rock-paper-scissors. I believe that the code works fine, but I can't check because it refuses to execute.I have looked extensively for answers on the internet, but can't seem to find any. I have very little experience in JavaScript, I am currently learning it off of Codecademybut I think that resource may be outdated as other websitesappear to have conflicting syntax. In short, what am I doing wrong, and which website has it right?
所以我开始学习 HTML/CSS/JavaScript 并在尝试制作一个非常简单的石头剪刀布版本时遇到了这个问题。我相信代码工作正常,但我无法检查,因为它拒绝执行。我在互联网上广泛寻找答案,但似乎找不到任何答案。我在 JavaScript 方面的经验很少,我目前正在从Codecademy学习它,但我认为该资源可能已经过时,因为其他网站似乎有冲突的语法。简而言之,我做错了什么,哪个网站做错了?
<html>
<head>
<title>R,P,S!</title>
<script type="text/javascript">
function whole(){
function game(play){
if (play="yes"){
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";}
else if(computerChoice <= 0.67) {
computerChoice = "paper";}
else {
computerChoice = "scissors";
}
function compare(choice1,choice2){
if (choice1==choice2){
compare(userChoice,computerChoice);
}
if (choice1=="rock"){
if (choice2=="scissors"){
document.getElementById("result").innerHTML="Rock wins";
}
else{
document.getElementById("result").innerHTML="Paper wins";
}
}
if (choice1=="paper"){
if (choice2=="rock"){
document.getElementById("result").innerHTML="Paper wins";
}
else{
document.getElementById("result").innerHTML="Scissors win";
}
}
if (choice1=="scissors"){
if (choice2=="paper"){
document.getElementById("result").innerHTML="Scissors win";
}
else{
document.getElementById("result").innerHTML="Rock wins";
}
}
};
compare(userChoice,computerChoice);
}
else{
document.writeln("<p>Thanks for playing! This was made by Alex</p>";)
}
}
var start = prompt ("Do you want to play?","Yes");}
</script>
</head>
<body style="text-align:center">
<h1>JavaScript on a webpage? This is madness!</h1>
<h2>Madness? THIS... IS... HTML!!!!</h2>
<button onclick="whole()">Try it out!</button>
<p id="result">Who won?</p>
</body>
</html>
**Edit: It appears that Codecademy's glossary agrees with other websites, they just haven't gotten around to editing their lessons yet.*
**编辑:Codecademy 的词汇表似乎与其他网站一致,他们只是还没有开始编辑他们的课程。*
**Edit: Here's my final little code for it. Enjoy its simplicity!*
**编辑:这是我最后的小代码。享受它的简单!*
<html>
<head>
<title>R,P,S!</title>
<script type="text/javascript">
function whole(){
function game(play){
if (play=="Yes"||play=="yes"){
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";}
else if(computerChoice <= 0.67) {
computerChoice = "paper";}
else {
computerChoice = "scissors";
}
function compare(choice1,choice2){
if (choice1==choice2){
alert("It was a tie!");
game("yes");
}
if (choice1=="rock"){
if (choice2=="scissors"){
document.getElementById("messages").innerHTML="";
document.getElementById("win").innerHTML="You win. Rock crushes scissors.";
document.getElementById("loss").innerHTML="";
}
else{
document.getElementById("messages").innerHTML="";
document.getElementById("loss").innerHTML="You lose. Paper smothers rock.";
document.getElementById("win").innerHTML="";
}
}
else if (choice1=="paper"){
if (choice2=="rock"){
document.getElementById("messages").innerHTML="";
document.getElementById("win").innerHTML="You win. Paper smothers rock.";
document.getElementById("loss").innerHTML="";
}
else{
document.getElementById("messages").innerHTML="";
document.getElementById("loss").innerHTML="You lose. Scissors cut paper.";
document.getElementById("win").innerHTML="";
}
}
else if (choice1=="scissors"){
if (choice2=="paper"){
document.getElementById("messages").innerHTML="";
document.getElementById("win").innerHTML="You win. Scissors cut paper.";
document.getElementById("loss").innerHTML="";
}
else{
document.getElementById("messages").innerHTML="";
document.getElementById("loss").innerHTML="You lose. Rock crushes scissors.";
document.getElementById("win").innerHTML="";
}
}
else{
alert("Very funny. Now do it right.");
game("yes");
}
};
compare(userChoice,computerChoice);
}
else{
document.getElementById("messages").innerHTML="Well alrighty then.";
document.getElementById("loss").innerHTML="";
document.getElementById("win").innerHTML="";
}
}
var start = prompt ("Do you want to play?","Yes");
game(start);}
</script>
<style>
body{
text-align:center;
}
#messages{
font-size:20px;
color: #00246B;
}
#win{
color: #29A329;
font-size:18px;
}
#loss{
color:#CC0000;
font-size:18px;
}
a{
text-decoration:none;
color:black;
}
a:hover{
font-size:125%;
color:#B20000;
}
button{
font-size:21px;
}
</style>
</head>
<body>
<a href="http://youtu.be/T8r3cWM4JII">
<h1>JavaScript on a webpage? This is madness!</h1>
<h2>Madness? THIS... IS... HTML!!!!</h2>
</a>
<button onclick="whole()">Try it out!</button>
<p id="messages">Who won?</p>
<p class="result"><span id="loss"></span><span id="win"></span></p>
</body>
</html>
回答by Arun Aravind
your code is syntatically wrong. put semicolon after paranthesis.
你的代码在语法上是错误的。在括号后加分号。
else {
document.writeln("<p>Thanks for playing! This was made by Alex</p>");
}
Now call the function game inside the closure at the end of function whole.
现在在整个函数的末尾调用闭包内的函数游戏。
ie
IE
function() whole(){
function game(play) {
// your logic
}
var start = prompt("Do you want to play?", "Yes");
game(start);
}
Your game logic is error-prone. recheck your logic.
你的游戏逻辑很容易出错。重新检查你的逻辑。
You are calling the function compare recursively infinitely when choice1 == choice2
当您无限递归地调用函数比较时 choice1 == choice2
just comment the line
只需注释该行
if(choice1 == choice2) {
// compare(userChoice, computerChoice);
}
it will work
它会起作用
回答by SomeShinyObject
You have some issues with your code:
您的代码有一些问题:
Assignment operator in if
statement:
if
语句中的赋值运算符:
if (play="yes")
This just assigns the string "yes"
to the variable play
. You should use a comparison operator:
这只是将字符串分配"yes"
给变量play
。您应该使用比较运算符:
if (play === "yes")
如果(播放 ===“是”)
Misplaced semi-colon:
错位的分号:
document.writeln("<p>Thanks for playing! This was made by Alex</p>";)
Should be:
应该:
document.writeln("<p>Thanks for playing! This was made by Alex</p>");