单击后,我的按钮保持消失

时间:2013-06-20 02:57:49

标签: javascript css

我在我的代码结尾处制作了一个摇滚,纸张,剪刀和按钮的游戏,然后在你点击它之后,它就消失了,继续重复该代码是没有意义的再一次。

function game(){ 
    var userChoice= prompt("Do you choose rock, paper, or scissors?");
    document.write("You chose " + userChoice + ". <br/>")
    var computerChoice= Math.random();
    if(computerChoice<=0.33){
        computerChoice="rock";
    }
    else if(computerChoice<=0.67){
        computerChoice="paper";
    }
    else{
        computerChoice="scissors";
    }
    document.write("The computer chose " + computerChoice + ". <br/>");
    if(userChoice==computerChoice){
        document.write("TIE!!!!");
    }
    else if(userChoice=="paper" && computerChoice=="rock"){
        document.write('<span style="color:red">You win!</span>');
    }
    else if(userChoice=="rock" && computerChoice=="scissors"){
        document.write('<span style="color:red">You win!</span>');
    }
    else if(userChoice=="scissors" && computerChoice=="paper"){
        document.write('<span style="color:red">You win!</span>');
    }
    else if(userChoice=="scissors" && computerChoice=="rock"){
        document.write('Sorry, you lose.');
    }
    else if(userChoice=="rock" && computerChoice=="paper"){
        document.write("Sorry, you lose.");
    }
    else if(userChoice=="paper" && computerChoice=="scissors"){
        document.write("Sorry, you lose.");
    }
    else if(userChoice=="Chuck Norris"){
        document.write("This program bows down to your superiority, YOU WIN!!!!");
    }
    document.write('<button type="button" style="display:block;"  onclick="game()">Play Again!</button>');

}
<body onload="game()">

2 个答案:

答案 0 :(得分:2)

将代码写入类似以下的元素或类似的元素。 Document.write会让东西消失。

<h1 id='myText'></h1>

document.getElementById('myText')。innerHTML ='我写的文件'

答案 1 :(得分:2)

忘掉document.write。使用div将内容写入。

<div id="mydiv"></div>

然后写这样的div:

document.getElementById("mydiv").innerHTML += "Sorry, you lose";

当然,您可以将“mydiv”放入变量并重复使用。

var mydiv = document.getElementById("mydiv");
mydiv.innerHTML += "Sorry, you lose";
mydiv.innerHTML += "Sorry again";