2016-01-22 30 views
1
<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> 

所以目前发生的是当我运行它时,它会提示我输入点数,然后显示两个按钮“Start!”和“买剑(2500)!”。然后点击开始后,下一页显示4张要购买的图片。如何在第二页上创建按钮?

我想要发生的是,在输入点数之后,我只想让它显示“开始!”按钮。然后在NEXT页面上显示图片的同一页面,我想显示“Buy Sword”按钮。

我明白为什么这样做,我只是不知道如何改变它。谁能帮我这个?

+0

你[真的不应该使用'document.write'(http://stackoverflow.com/questions/802854 /为什么 - 是 - 文档写入认为,一个坏实践)。除此之外,一旦您获得正确的DOM操作,在一页上完成这一切可能会更容易。 –

回答

3

您必须关闭p标签,你应该避免文件撰写

<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> 
+0

谢谢。到目前为止,教授只使用了document.write,所以我没有意识到这是糟糕的,甚至还有其他方法。 – Jetridder11

+0

没问题。你可以查看Mike的链接来阅读更多关于它的信息。如果你对答案感到满意,不要忘记点击复选标记:) –

相关问题