我的按钮在墙上程序中的99瓶啤酒中不断消失

时间:2013-12-11 03:32:28

标签: javascript

嘿,我真的不知道为什么我的按钮不断消失在我身上,我还应该提到我在2个月内没有做任何javascript,所以我可能会有点生疏。

<html>
<head></head>
<body>
    <script language = "JavaScript">
    function Count(){
        var x = "98";
        x--;
        var y = document.write(x + "<p> Bottles of beer on the wall </p>" + x + "<p> bottles of beer You take one down pass it around</p>" + x-- + "<p> of beer on the wall!</p>");
        document.getElementById('text').innerHTML = y;
    }
    </script>

    <form>
        <input type="button" value="Count" onClick="Count()">
    </form>
    <p id="text">99 Bottles of beer on the wall 99 Bottles of beer you take one down pass it around 98 bottles of beer on the wall!</p>
</body>
</html>

3 个答案:

答案 0 :(得分:2)

移除覆盖正文的 document.write

var y = x + "<p> Bottles of beer on the wall </p>" + x + 
            "<p> bottles of beer You take one down pass it around</p>" + x-- + 
            "<p> of beer on the wall!</p>";
document.getElementById('text').innerHTML = y;

在这个答案中你可以找到What actually document.write does

答案 1 :(得分:0)

这是因为document.write()会覆盖你的身体。 请改用:

var y = (x + "<p> Bottles of beer on the wall </p>" + x + "<p> bottles of beer You take one down pass it around</p>" + x-- + "<p> of beer on the wall!</p>");
document.getElementById('text').innerHTML = y;

答案 2 :(得分:0)

主要是在这种情况下不使用document.write()。这实际上是写入浏览器。试试这个:

<html>
<head></head>
<body>
    <script language = "JavaScript">
    var x = "98";
    function Count(){
        next = x - 1;
        var y = x + " bottles of beer on the wall " + x + " bottles of beer You take one down pass it     around " + next + " of beer on the wall!";
        document.getElementById('text').innerHTML = y;
        x--;
        return false;
    }
    </script>

    <form action="#" method="post">
        <input type="button" value="Count" onClick="Count()">
    </form>
    <p id="text">99 Bottles of beer on the wall 99 Bottles of beer you take one down pass it around 98     bottles of beer on the wall!</p>
</body>
</html>

您在递减x的方式方面也遇到了一些问题。