每当我按下任何按钮,比如石头、纸、剪刀,我都希望代码生成一个介于1和3之间的随机数。我不确定我做错了什么,因为我是编程新手。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Rock Paper Scissors</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script>
display.innerHTML=Math.floor((Math.random()*3)+1);
}
</script>
</head>
<body>
<form>
<input id="rock" type="button" onclick="Random()" value="Rock"/>
<input id="paper" type="button" onclick="Random()" value="Paper"/>
<input id="scissors" type="button" onclick="Random()" value="Scissors"/>
</form>
<span id="display"></span>
</body>
</html>
您忘记了onClick事件的函数名称。使用以下内容调整脚本标记中的代码。
<script>
function Random() {
document.getElementById('display').innerHTML = Math.floor((Math.random() * 3) + 1);
}
</script>
jsFiddle
我想您错过了函数定义。
<script>
function Random(){
display.innerHTML=Math.floor((Math.random()*3)+1);
}
</script>
尝试用此替换您的。关于Jvascript函数的更多信息
您的工作代码在这里
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Rock Paper Scissors</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script>
function Random(){
display.innerHTML=Math.floor((Math.random()*3)+1);
}
</script>
</head>
<body>
<form>
<input id="rock" type="button" onclick="Random()" value="Rock"/>
<input id="paper" type="button" onclick="Random()" value="Paper"/>
<input id="scissors" type="button" onclick="Random()" value="Scissors"/>
</form>
<span id="display"></span>
</body>
</html>
您正试图调用一个未在任何位置定义的函数。