且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JS:1.1,消息框(alert,confirm,prompt)

更新时间:2022-09-13 12:15:55

ylbtech:消息框(alert,confirm,prompt)

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

实例

警告框
带有折行的警告框
确认框
提示框

警告框

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

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

语法:

alert("文本")

确认框

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

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

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

语法:

confirm("文本")

提示框

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

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

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

语法:

prompt("文本","默认值")
1.1.1,警告框返回顶部
1.1.1.1,警告框
JS:1.1,消息框(alert,confirm,prompt)
<html>
<head>
<script type="text/javascript">
function disp_alert()
{
alert("我是警告框!!")
}
</script>
</head>
<body>

<input type="button" onClick="disp_alert()" value="显示警告框" />

</body>
</html>
JS:1.1,消息框(alert,confirm,prompt)
1.1.1.2,带有折行的警告框
JS:1.1,消息框(alert,confirm,prompt)
<html>
<head>
<script type="text/javascript">
function disp_alert()
{
alert("再次向您问好!在这里,我们向您演示" + '\n' + "如何向警告框添加折行。")
}
</script>
</head>
<body>

<input type="button" onClick="disp_alert()" value="显示警告框" />

</body>
</html>
JS:1.1,消息框(alert,confirm,prompt)
1.1.2,确认框返回顶部
JS:1.1,消息框(alert,confirm,prompt)
<html>
<head>
<script type="text/javascript">
function show_confirm()
{
var r=confirm("按下一个按钮!");
if (r==true)
  {
  alert("按下 确定!");
  }
else
  {
  alert("按下 取消!");
  }
}
</script>
</head>
<body>

<input type="button" onClick="show_confirm()" value="展示一个确认框" />

</body>
</html>
JS:1.1,消息框(alert,confirm,prompt)
1.1.3,提示框返回顶部
JS:1.1,消息框(alert,confirm,prompt)
<html>
<head>
<script type="text/javascript">
function disp_prompt()
  {
  var name=prompt("请输入您的名字","Bill Gates")
  if (name!=null && name!="")
    {
    document.write("你好!" + name + " 今天过得怎么样?")
    }
  }
</script>
</head>
<body>

<input type="button" onClick="disp_prompt()" value="显示提示框" />

</body>
</html>
JS:1.1,消息框(alert,confirm,prompt)

 

如果你想把某门功课学好,除非花费更多的时间,并没有更好的方法。23:26 2013/1/8

 

本文转自ylbtech博客园博客,原文链接:http://www.cnblogs.com/ylbtech/archive/2013/01/08/2852068.html,如需转载请自行联系原作者