如何在第二页上创建按钮?

时间:2016-01-22 22:19:59

标签: javascript html button

    <html>
        <head>
            <title>The greatest MMO you will ever play</title>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
        </head>
        <body>
            <script>
                function buyStuffWithPoints()
                {
                 var points=prompt("How many points have you earned?");
                 document.write("<p>Buy your items now and prepare for battle! Choose wisely.<p>" );
                 document.write("<p><img src = 'sword.jpg'/><p>");
                 document.write("<p><img src = 'Waterskin.jpg' /><p>");
                 document.write("<p><img src = 'charm.jpg' /><p>");
                 document.write("<p><img src = 'Phone.jpg' /><p>");

                }           

            </script>

            <input type="button" onclick="buyStuffWithPoints()" value="Start!" />
            <div>
           <input type="button" onclick="buyStuffWithPoints()" value="Buy Sword(2500)!" />
            </div>
        </body>
    </html>

所以目前发生的事情是当我运行它时,它会提示我输入点数,然后它会显示两个按钮,&#34;开始!&#34;和&#34;买剑(2500)!&#34;。然后点击开始后,下一页显示要购买的项目的4张图片。

我想要发生的是,在我输入分数之后,我只希望它显示&#34;开始!&#34;按钮。然后在NEXT页面上,图片显示的同一页面,我想显示&#34; Buy Sword&#34;按钮。

我明白为什么要这样做,我根本不知道如何改变它。任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分:3)

您必须关闭 p 标记,并且应该避免 document.write

<html>
    <head>
        <title>The greatest MMO you will ever play</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <script>
            function buyStuffWithPoints() {

                var points = prompt("How many points have you earned?");

                var html = "<p>Buy your items now and prepare for battle! Choose wisely.</p>"
                        + "<p><img src = 'sword.jpg'/></p>"
                        + "<div><input type=\"button\" onclick=\"buyStuffWithPoints()\" value=\"Buy Sword(2500)!\" /></div>"
                        + "<p><img src = 'Waterskin.jpg' /></p>"
                        + "<p><img src = 'charm.jpg' /></p>"
                        + "<p><img src = 'Phone.jpg' /></p>"

                document.body.innerHTML = html;
            }
        </script>

        <input type="button" onclick="buyStuffWithPoints()" value="Start!" />
    </body>
</html>