猿问

如何在 javascript/html 中用新字符串替换字符串?

我有一台最近制作的老虎机,但我想让它看起来更好一点。

https://codesandbox.io/s/slot-machine-xip7x?file=/index.html:0-1480(抱歉,我在将代码复制并粘贴到代码示例中时遇到问题:( 我对此很陌生地点。)

这是该代码的结果:https://xip7x.csb.app/

每次滚动时,屏幕左侧都会出现一个项目符号点,结果位于中间。当您滚动多次时,新结果只会添加到已有结果中。任何人都可以做到这一点,这样就没有要点,并且每次滚动时它都会替换之前的滚动条并仅显示新的滚动条,因此页面上始终只有一个滚动条。我是编码新手,很想完成这个项目。如果有人能帮助我并解释他们做了什么,我会很高兴。谢谢!


哆啦的时光机
浏览 93回答 1
1回答

叮当猫咪

这是你想要的吗?&nbsp;var firstx = ["X", "O", "L", "L"];&nbsp; &nbsp; &nbsp; var secondx = ["X", "O", "L", "L"];&nbsp; &nbsp; &nbsp; var thirdx = ["X", "O", "L", "L"];&nbsp; &nbsp; &nbsp; function randIndex() {&nbsp; &nbsp; &nbsp; &nbsp; var randIndex1 = Math.floor(Math.random() * 4);&nbsp; &nbsp; &nbsp; &nbsp; var randIndex2 = Math.floor(Math.random() * 4);&nbsp; &nbsp; &nbsp; &nbsp; var randIndex3 = Math.floor(Math.random() * 4);&nbsp; &nbsp; &nbsp; &nbsp; var first = firstx[randIndex1];&nbsp; &nbsp; &nbsp; &nbsp; var second = secondx[randIndex2];&nbsp; &nbsp; &nbsp; &nbsp; var third = thirdx[randIndex3];&nbsp; &nbsp; &nbsp; &nbsp; var btn = document.getElementById("btn");&nbsp; &nbsp; &nbsp; &nbsp; var sent = document.getElementById("sent");&nbsp; &nbsp; &nbsp; &nbsp; var result = first + " " + second + " " + third;&nbsp; &nbsp; &nbsp; &nbsp; if (result == "X X X") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("CONGRATULATIONS! YOU WIN!");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; sent.innerHTML ="<li>" + result + "</li>";&nbsp; &nbsp; &nbsp; &nbsp; btn.innerText = "Roll Again";&nbsp; &nbsp; &nbsp; }&nbsp; font-family: helvetica;&nbsp; &nbsp; &nbsp; &nbsp; text-align: center;&nbsp; &nbsp; &nbsp; &nbsp; -webkit-text-fill-color: black;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; body {&nbsp; &nbsp; &nbsp; &nbsp; background-color: lightcoral;&nbsp; &nbsp; &nbsp; }&nbsp;<!DOCTYPE html><html>&nbsp; <head>&nbsp; &nbsp; <title>Slot Machine</title>&nbsp; &nbsp; <meta charset="UTF-8" />&nbsp; </head>&nbsp; <body>&nbsp;<h1>Slot Machine</h1>&nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; Welcome to the slot machine! To play, click the "Roll" button. If you get&nbsp; &nbsp; &nbsp; three X's, YOU WIN! Have fun and Enjoy your new gambling addiction!&nbsp; &nbsp; </p>&nbsp; &nbsp; <br />&nbsp; &nbsp; <button id="btn" onclick="randIndex();">&nbsp; &nbsp; &nbsp; Roll&nbsp; &nbsp; </button>&nbsp; &nbsp; <ul id="sent"></ul>&nbsp; &nbsp; <br /><br />&nbsp; &nbsp; <hr />&nbsp; &nbsp; <br /><br />&nbsp; &nbsp; &nbsp; </body></html>
随时随地看视频慕课网APP

相关分类

Html5
我要回答