【发布时间】:2018-03-19 11:25:04
【问题描述】:
在 const 内:
<h2 className="logo" id="salt">{randomtext}</h2>
如何让随机文本显示为“hi”、“hello”、“yes!”以显示在触发器上(比如说一个按钮左右)
function randomtext() { ??
}
【问题讨论】:
标签: javascript reactjs ecmascript-6
在 const 内:
<h2 className="logo" id="salt">{randomtext}</h2>
如何让随机文本显示为“hi”、“hello”、“yes!”以显示在触发器上(比如说一个按钮左右)
function randomtext() { ??
}
【问题讨论】:
标签: javascript reactjs ecmascript-6
你应该使用Math.random js函数在定义的数组中有一个随机值,这里有一个简单的例子:
function getRandomText() {
const txts = ["hello", "bye", "see you", "mmm..."]
let index = Math.round(Math.random() * (txts.length - 1))
return txts[index]
}
let randomText = getRandomText()
$("#rText").html(randomText)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="rText"></div>
【讨论】: