我试图在单击按钮时显示正面或反面。我不确定我哪里错了。这似乎是一件很简单的事情,但也许我错过了一些东西?这是我所拥有的。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Assignment 9</title>
<link href="images/avatar.png" rel="shortcut icon" type="image/png">
<link href="css/stylesheet.css" rel="stylesheet" type="text/css">
<script src="js/javascript.js" type="text/javascript"></script>
<style type="text/css">
.auto-style1 {
text-align: center;
}
</style>
<script type="text/javascript">
</script>
</head>
<body>
<header>
</header>
<aside>
</aside>
<div id="main">
<h1> Arrays and Coditional Statements</h1>
<h2 class="auto-style1"> Quote of the Day</h2>
<h2 class="auto-style1">Flip a coin </h2>
<script>
<script>
function myFunction1{
var x =Math.floor(Math.random()*2);
if ( x==0){
document.getElementById("Coin").innerHTML= "Heads";
}
else{
document.getElementById("Coin").innerHTML= "Tails";
}}
myFunction1();
</script>
<button type="button" onclick="myFunction1" id="Coin"> CLick ME</button>
</div>
<footer>
</footer>
</body>
</html>
最佳答案
您可以使用这一行 JavaScript 来获取 heads
或 tails
console.log(Math.random() >= 0.5 ? "heads" : "tails")
关于javascript - 使用 JavaScript 翻转硬币,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33619443/