繁体   English   中英

Javascript alert()函数歧义

[英]Javascript alert() function ambiguity

我想在显示警报消息之前更改html正文的背景颜色。 这是我的代码:

var guess;
  var count=1;
  var color=["red","green","blue","yellow","indigo","purple","black","cyan","violet"];
  var num=Math.floor(Math.random()*9);
  alert("I am thinking of "+color[num]+" color");
  function do_game()
  {
    do {
      guess=get();
      if(color.indexOf(guess.toLowerCase())==-1)
      {
        alert("Sorry, I don't recognize your color");
        continue;
      }
      if(guess.toLowerCase()>color[num])
      {
        alert("Your color is alphabetically higher than my color!");
        count++;
        continue;
      }
      if(guess.toLowerCase()<color[num])
      {
        alert("Your color is alphabetically lower than my color!");
        count++;
        continue;
      }
    } while (guess.toLowerCase()!=color[num]);
    document.body.style.background=color[num];
    alert("Congratulations! You guessed the color!\n\nIt took you "+count+" guesses\n\nYou can see the color in the background");
  }

在这里,即使我在关闭警报之前编写了更改颜色的代码,背景颜色也仅在关闭警报后才会更改。 真的很困惑。 我正在使用Safari浏览器

document.body.style.background=color[num];

之前

alert("I am thinking of "+color[num]+" color");

并使用jQuery。 适用于我的:Safari,Chrome(即11和Firefox)

$(document).ready(function() {
    var guess;
    var count=1;
    var color=["red","green","blue","yellow","indigo","purple","black","cyan","violet"];
    var num=Math.floor(Math.random()*9);
    document.body.style.background=color[num];
    alert("I am thinking of "+color[num]+" color");
    function do_game()
    {
    do {
      guess=get();
      if(color.indexOf(guess.toLowerCase())==-1)
      {
        alert("Sorry, I don't recognize your color");
        continue;
      }
      if(guess.toLowerCase()>color[num])
      {
        alert("Your color is alphabetically higher than my color!");
        count++;
        continue;
      }
      if(guess.toLowerCase()<color[num])
      {
        alert("Your color is alphabetically lower than my color!");
        count++;
        continue;
      }
    } while (guess.toLowerCase()!=color[num]);
    document.body.style.background=color[num];
    alert("Congratulations! You guessed the color!\n\nIt took you "+count+" guesses\n\nYou can see the color in the background");
    }
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM