【问题标题】:Random text in the DOM? [duplicate]DOM中的随机文本? [复制]
【发布时间】:2018-03-19 11:25:04
【问题描述】:

在 const 内:

<h2 className="logo" id="salt">{randomtext}</h2>

如何让随机文本显示为“hi”、“hello”、“yes!”以显示在触发器上(比如说一个按钮左右)

function randomtext() { ??
}

【问题讨论】:

    标签: javascript reactjs ecmascript-6


    【解决方案1】:

    你应该使用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>

    【讨论】:

      猜你喜欢
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 2010-10-28
      • 2015-03-12
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      • 2016-06-13
      相关资源
      最近更新 更多