gpt4 book ai didi

javascript - html body onload 不起作用

转载 作者:行者123 更新时间:2023-11-28 04:23:26 24 4
gpt4 key购买 nike

我有一个简单的 JavaScript 代码,但它不起作用。我的代码是:

<html>
<head>
<title>JavaScript</title>
<script>
var target;
var guess_input;
var guesses = 0;
var finished = false;
var colors = ["black","blue","green","purple","red","white","yellow"];



function do(){


var random = Math.random();
var index = Math.floor(random * 7);
target = colors[index];
while(!finished){
guess_input = prompt("I am thinking of a color : black, blue, green, purple, red, white \n\n" + "What color am i thinking of?");
guesses++;
finished = check();   
}
var myBody = document.getElementsByTagName("body")[0];
myBody.style.background = target;
}

function check(){
if (guess_input > target){
alert("your color is alphabetically higher than mine");
return false;
}

if (guess_input < target){
alert("your color is alphabetically lower than mine");
return false;
}

if (guess_input == target){
alert("your color is correct! it tooks you" + guesses "guesses to finish the game!");
return true;
}
else {
alert("Sorry, I do not recoginize your color");
return false;
}
</script>
</head>
<body onload = "do()">

</body>

</html>

错误消息是:Uncaught SyntaxError: Unexpected token dopart2.html:53 Uncaught SyntaxError: Unexpected token ),即“function do()”,那么问题出在哪里呢?谢谢!

最佳答案

您缺少用于函数检查的右括号并将 do 函数名称更改为其他函数,例如 doSomething

    <html>
<head>
<title>JavaScript</title>
<script>
var target;
var guess_input;
var guesses = 0;
var finished = false;
var colors = ["black","blue","green","purple","red","white","yellow"];
function doSomething(){


var random = Math.random();
var index = Math.floor(random * 7);
target = colors[index];
while(!finished){
guess_input = prompt("I am thinking of a color : black, blue, green, purple, red, white \n\n" + "What color am i thinking of?");
guesses++;
finished = check();
}
var myBody = document.getElementsByTagName("body")[0];
myBody.style.background = target;
}

function check(){
if (guess_input > target){
alert("your color is alphabetically higher than mine");
return false;
}

if (guess_input < target){
alert("your color is alphabetically lower than mine");
return false;
}

if (guess_input == target){
alert("your color is correct! it tooks you" + guesses "guesses to finish the game!");
return true;
}
else {
alert("Sorry, I do not recoginize your color");
return false;
}
}
</script>
</head>
<body onload="doSomething()">

</body>

</html>

关于javascript - html body onload 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45260141/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com