我需要一个按钮不要消失

时间:2015-04-01 00:43:02

标签: javascript button random

我正在制作一个带有按钮的javascript代码,当我点击它时,它会显示5个符号中的一个,但是当我点击按钮时,它会显示随机符号,但按钮会消失。我是javascript的新手,所以我可以帮忙吗?

<script>

function slots()
{
    var slot1 = Math.floor(Math.random()*5);
    if (slot1 == 0) {
        document.write("\u2663");
    }
    if (slot1 == 1) {
        document.write("\u2665");
    }
    if (slot1 == 2) {
        document.write("\u2666");
    }
    if (slot1 == 3) {
        document.write("\u2660");
    }
    if (slot1 == 4) {
        document.write("7");
    }
}

</script>
<button type="button" value="Spin" name="SPIN"onClick="slots(); return false;"></button>

2 个答案:

答案 0 :(得分:2)

当你写document.write()屏幕刷新时,我想你可以这样做:

<script>

function slots()
{
    var slot1 = Math.floor(Math.random()*5);
    if (slot1 == 0) {
        document.getElementById('value').innerHTML = "\u2663";
    }
    if (slot1 == 1) {
        document.getElementById('value').innerHTML = "\u2665";
    }
    if (slot1 == 2) {
        document.getElementById('value').innerHTML = "\u2666";
    }
    if (slot1 == 3) {
        document.getElementById('value').innerHTML = "\u2660";
    }
    if (slot1 == 4) {
        document.getElementById('value').innerHTML = "7";
    }

}
</script>

<button type="button" value="Spin" name="SPIN" onClick="slots();">Click</button>
<span id="value"></span>

答案 1 :(得分:0)

otrebla代码的略微优化版本,请参阅实际操作:

&#13;
&#13;
function slots() {
  var slot1 = Math.floor(Math.random() * 5);
  var value = document.getElementById('value');
  switch (slot1) {
    case 0:
      value.innerHTML = "\u2663";
      break;
    case 1:
      value.innerHTML = "\u2665";
      break;
    case 2:
      value.innerHTML = "\u2666";
      break;
    case 3:
      value.innerHTML = "\u2660";
      break;
    case 4:
      value.innerHTML = "7";
      break;
  }
}
&#13;
<button type="button" value="Spin" name="SPIN" onClick="slots();">Click</button>
<span id="value"></span>
&#13;
&#13;
&#13;