1. 最简版本
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
div.status {
margin: 5px;
background-color: lightgrey;
width: 200px;
height: 160px;
}
div.in {
float: left;
width: 90px;
height: 90px;
}
p.pIn {
text-align: center;
}
img {
height: 80px;
width: 80px;
display: block;
margin: 5px;
border: 2px solid;
}
</style>
</head>
<body>
<select id="numb" name="numb">
<option value="0">剪刀</option>
<option value="1">石头</option>
<option value="2">布</option>
</select><br><br>
<button type="button" onclick="myFunction()">出拳</button><br>
<div class="status">
<div class="in">
<p class="pIn">您</p>
<P id="xPicture"></P>
</div>
<div class="in">
<p class="pIn">对手</p>
<P id="yPicture"></P>
</div>
</div >
<p id="result"></p>
<script>
function myFunction() {
var x, text;
x = document.getElementById("numb").value;
y = Math.floor(Math.random() * 3);
var yObject = new Array();
yObject[0] = "剪刀";
yObject[1] = "石头";
yObject[2] = "布";
imgX = "<img " + "src="+"\"\./images/a"+x+"\.png\"" +" >"
imgY = "<img " + "src="+"\"\./images/a"+y+"\.png\"" +" >"
z = x - y;
if (z < 0){
z = z +3
};
switch (z){
case 0:
text="平局";
break;
case 1:
text="你赢了";
break;
case 2:
text="你输了";
break;
}
document.getElementById("result").innerHTML = text;
document.getElementById("xPicture").innerHTML = imgX;
document.getElementById("yPicture").innerHTML = imgY;
document.getElementById("yString").innerHTML = yObject[y];
}
</script>
</body>
</html>
- 效果
2. 网页版
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
margin: 0;
}
ul {
list-style-type: none;
margin: 10px;
padding: 0;
width: 25%;
background-color: #f1f1f1;
position: fixed;
height: 100%;
overflow: auto;
}
div.out {
margin-left:26%;
background-color: #f1f1f1;
margin-top: 10px;
padding: 0;
height: 1000px;
}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}
li a.active {
background-color: #4CAF50;
color: white;
}
li a:hover:not(.active) {
background-color: #555;
color: white;
}
div.status {
margin: 0 auto;
background-color: lightgrey;
width: 190px;
height: 160px;
}
div.in {
float: left;
width: 90px;
height: 90px;
}
p.pIn {
text-align: center;
}
img {
height: 80px;
width: 80px;
display: block;
margin: 5px;
border: 2px solid;
}
</style>
</head>
<body>
<div id="header" style="background-color:#adbfc7;">
<h1 style="margin-bottom:0; text-align: center;">html练习册</h1></div>
<ul>
<li><a class="active" href="./index.html">主页</a></li>
<li><a href="./game01.html">剪刀石头布</a></li>
<li><a href="#contact">决斗吧</a></li>
<li><a href="#about">关于</a></li>
</ul>
<div class="out" >
<div style="text-align: center;">
<select id="numb" name="numb" style="margin: 10px;">
<option value="0">剪刀</option>
<option value="1">石头</option>
<option value="2">布</option>
</select><br><br>
<button type="button" onclick="myFunction()">出拳</button><br><br>
<div class="status">
<div class="in">
<p class="pIn">您</p>
<P id="xPicture"></P>
</div>
<div class="in">
<p class="pIn">对手</p>
<P id="yPicture"></P>
</div>
</div >
<p id="result">结果</p>
</div>
</div>
<script>
function myFunction() {
var x, text;
x = document.getElementById("numb").value;
y = Math.floor(Math.random() * 3);
var yObject = new Array();
yObject[0] = "剪刀";
yObject[1] = "石头";
yObject[2] = "布";
imgX = "<img " + "src="+"\"\./images/a"+x+"\.png\"" +" >"
imgY = "<img " + "src="+"\"\./images/a"+y+"\.png\"" +" >"
z = x - y;
if (z < 0){
z = z +3
};
switch (z){
case 0:
text="平局";
break;
case 1:
text="你赢了";
break;
case 2:
text="你输了";
break;
}
document.getElementById("result").innerHTML = text;
document.getElementById("xPicture").innerHTML = imgX;
document.getElementById("yPicture").innerHTML = imgY;
document.getElementById("yString").innerHTML = yObject[y];
}
</script>
</body>
</html>
- 结果