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> |
- 结果


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律