qq_27520301
qq_27520301
2017-11-05 10:55

小白问题急求!生成随机颜色的正方形,代码如下,修改要求:新生成的颜色不能与上一个颜色重复

  • utf-8
  • html5
  • 函数

<!DOCTYPE html>



randomColor #square{ background-color: red; margin: auto; height: 200px; width: 200px; }


var colors = ['red','blue','yellow','green','black','purple']; function setColor(){ var n =0; var m = Math.floor(Math.random()*6); if (m == n){ setColor(); } else{ document.getElementById('square').style.backgroundColor = colors[m]; document.getElementById('para').innerHTML = colors[m]; } } function control(){ var button = document.getElementById("button"); if (button.value =="stop"){ clearInterval(change); button.value = "go on"; } else{ change = setInterval(setColor,500); button.value ="stop"; } }



  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答