JavaScript未在我的HTML中执行
所以我开始学习HTML/CSS/JavaScript,在尝试制作一个非常简单的石头剪刀版本时遇到了这个问题。我相信代码工作正常,但我无法检查,因为它拒绝执行。我在互联网上广泛查找答案,但似乎找不到任何答案。我在JavaScript方面的经验很少,我目前正在学习它,但我认为该资源可能已经过时,因为它似乎有冲突的语法。简而言之,我做错了什么,哪个网站做对了?JavaScript未在我的HTML中执行,javascript,html,css,execute,Javascript,Html,Css,Execute,所以我开始学习HTML/CSS/JavaScript,在尝试制作一个非常简单的石头剪刀版本时遇到了这个问题。我相信代码工作正常,但我无法检查,因为它拒绝执行。我在互联网上广泛查找答案,但似乎找不到任何答案。我在JavaScript方面的经验很少,我目前正在学习它,但我认为该资源可能已经过时,因为它似乎有冲突的语法。简而言之,我做错了什么,哪个网站做对了? <html> <head> <title>R,P,S!</title> <
<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>
R、 P,S!
函数整(){
功能游戏(游戏){
如果(play=“yes”){
var userChoice=prompt(“您选择石头、布还是剪刀?”);
var computerChoice=Math.random();
如果(计算机选择<0.34){
computerChoice=“rock”}
否则,如果(computerChoice您的代码语法错误,请在语法后加分号
else {
document.writeln("<p>Thanks for playing! This was made by Alex</p>");
}
您的游戏逻辑容易出错。请重新检查您的逻辑
当choice1==choice2
请评论这句话
if(choice1 == choice2) {
// compare(userChoice, computerChoice);
}
它将起作用您的代码有一些问题:
if
语句中的赋值运算符:
if (play="yes")
这只是将字符串“yes”
分配给变量play
。您应该使用比较运算符:
如果(播放==“是”)
放错分号:
document.writeln("<p>Thanks for playing! This was made by Alex</p>";)
document.writeln(“感谢您的播放!这是Alex制作的””;)
应该是:
document.writeln("<p>Thanks for playing! This was made by Alex</p>");
document.writeln(感谢您的播放!这是Alex制作的)”;
发生了什么事?当我按下按钮时,JavaScript没有执行。你应该打开浏览器的开发控制台,检查是否有任何错误消息。例如,看看“这是Alex制作的”;)“你的函数除了询问你是否想玩外,什么都不做。啊,我显然有一个意外的”}“。这应该很有趣。同样,比较运算符是“==”而不是“=”…函数内的游戏更改如果(play=“yes”)为play==”yes”谢谢!这就成功了。我知道这个游戏不是很好。只有在平局的情况下才会执行比较。我稍后会添加回退,这只是为了测试HTML.Dud中JS的集成。我说过,当平局出现时,您当前的语句会递归地独立调用函数compare。出现堆栈溢出和解释器退出了,好吧。这应该是game()
而不是compare(userChoice,computerChoice)
总之。谢谢你提醒我注意它。已经说过了,但无论如何谢谢!还有,不是比较运算符“==”?哦,好的。这两个都在这个例子中起作用。我甚至不知道它的存在,谢谢你给我看。
document.writeln("<p>Thanks for playing! This was made by Alex</p>";)
document.writeln("<p>Thanks for playing! This was made by Alex</p>");