显示提示框

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <h1>JavaScript Prompt</h1>
  6.  
  7. <button onclick="myFunction()">试一试</button>
  8.  
  9. <p id="demo"></p>
  10.  
  11. <script>
  12. function myFunction() {
  13. var txt;
  14. var person = prompt("请输入您的名字:", "哈利波特");
  15. if (person == null || person == "") {
  16. txt = "用户取消输入";
  17. } else {
  18. txt = "你好," + person + "!今天过得好吗?";
  19. }
  20. document.getElementById("demo").innerHTML = txt;
  21. }
  22. </script>
  23.  
  24. </body>
  25. </html>