【问题标题】:How to get a function to run again on 2nd and 3rd (and so on) click of a html button?如何让函数在第二次和第三次(等等)单击 html 按钮时再次运行?
【发布时间】:2021-09-06 03:55:31
【问题描述】:

我试图基本上只是制作一个脚本,在单击按钮后翻转硬币,其中 0 = 泰国菜和 1 = 越南菜。随机数总是为 1(泰国菜 一些理由)。当我单击按钮时,它不会不断变化。会很感激 作为初学者的任何帮助谢谢!

<h1> What's it going to be? </h1>
    <input type="button" id="random" value="Your food awaits" onclick="foodPicker()" >

<h3 id="food"> </h3>


function foodPicker(){
  let foodNum = Math.floor(Math.random() * 2)
  let result = ''
    if (foodNum = 1) {
        result = 'Thai';
    }
        else {
             result = 'Viet';
        }
  document.getElementById('food').innerHTML = result;
}


【问题讨论】:

    标签: javascript html function button


    【解决方案1】:

    if (foodNum = 1) { 应该是(foodNum == 1) {

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多