• JavaScript 消息框


    可以在 JavaScript 中创建三种消息框:警告框、确认框、提示框。

    警告框

    警告框经常用于确保用户可以得到某些信息。

    当警告框出现后,用户需要点击确定按钮才能继续进行操作。

    语法:

    alert("文本")
    例如:
     1 <html>
     2 <head>
     3 <script type="text/javascript">
     4 function disp_alert()
     5 {
     6 alert("再次向您问好!在这里,我们向您演示" + '
    ' + "如何向警告框添加折行。")
     7 }
     8 </script>
     9 </head>
    10 <body>
    11 
    12 <input type="button" onclick="disp_alert()" value="显示警告框" />
    13 
    14 </body>
    15 </html>

    确认框

    确认框用于使用户可以验证或者接受某些信息。

    当确认框出现后,用户需要点击确定或者取消按钮才能继续进行操作。

    如果用户点击确认,那么返回值为 true。如果用户点击取消,那么返回值为 false。

    语法:

    confirm("文本")
    例如:
     1 <html>
     2 <head>
     3 <script type="text/javascript">
     4 function show_confirm()
     5 {
     6 var r=confirm("Press a button!");
     7 if (r==true)
     8   {
     9   alert("You pressed OK!");
    10   }
    11 else
    12   {
    13   alert("You pressed Cancel!");
    14   }
    15 }
    16 </script>
    17 </head>
    18 <body>
    19 
    20 <input type="button" onclick="show_confirm()" value="Show a confirm box" />
    21 
    22 </body>
    23 </html>

    提示框

    提示框经常用于提示用户在进入页面前输入某个值。

    当提示框出现后,用户需要输入某个值,然后点击确认或取消按钮才能继续操纵。

    如果用户点击确认,那么返回值为输入的值。如果用户点击取消,那么返回值为 null。

    语法:

    prompt("文本","默认值")
    例如:
     1 <html>
     2 <head>
     3 <script type="text/javascript">
     4 function disp_prompt()
     5   {
     6   var name=prompt("请输入您的名字","Bill Gates")
     7   if (name!=null && name!="")
     8     {
     9     alert("你好!" + name + " 今天过得怎么样?");
    10     document.write("你好!" + name + " 今天过得怎么样?")
    11     }
    12   }
    13 </script>
    14 </head>
    15 <body>
    16 
    17 <input type="button" onclick="disp_prompt()" value="显示提示框" />
    18 
    19 </body>
    20 </html>
  • 相关阅读:
    Java应用调优指南之-工具篇
    2016第18周四
    2016第18周三
    IE下JS接受ActiveX控件方法
    2016第18周一
    Jquery插件写法及extentd函数
    2016第17周六
    嘛:如何远视 还有遥远的未来
    寻找失踪的整数数组(Find the missing integer)
    Cocos2d-x 2地图步行实现:SPFA算法
  • 原文地址:https://www.cnblogs.com/gmblog/p/3826889.html
Copyright © 2020-2023  润新知