JavaScript消息框

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

经常用于确保用户可以得到某些信息。当警告框出现后,用户需要点击确定按钮才能继续进行操作。
语法:alert("文本")
举例如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript警告框</title>
<script type="text/javascript">
function DisplayAlert()
{
  alert(
"我是一个警告框!");
}
</script>
</head>
<body>
<input type="button"  onclick="DisplayAlert()" value="点击显示警告框"/>
</body>
</html>

第二:确认框
用于使用户可以验证或者接受某些信息。当确认框出现后,用户需要点击确定或者取消按钮才能继续进行操作。如果用户点击确认,那么返回值为 true。如果用户点击取消,那么返回值为 false。
语法:confirm("文本")
举例如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript确认框</title>
<script type="text/javascript">
function DisplayConfirm()
{
  
var r=confirm("点击我!");
  
if(r==true)
  {
    document.write(
"返回的是True");
  }
  
else
  {
    document.write(
"返回的是false");
  }
}
</script>
</head>
<body>
<input type="button" onclick="DisplayConfirm()" value="JavaScript确认框"/>
</body>
</html>
第三:提示框
经常用于提示用户在进入页面前输入某个值。
当提示框出现后,用户需要输入某个值,然后点击确认或取消按钮才能继续操纵。如果用户点击确认,那么返回值为输入的值。如果用户点击取消,那么返回值为 null。
语法:prompt("文本","默认值")
举例如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript提示框</title>
<script type="text/javascript">
function DisplayPrompt()
{
  
var name=prompt("请输入你的名字","无为");
  
if(name!=null&&name!="")
  {
    document.write(
"Hello"+name+"!How are you today?");
  }
}
</script>
</head>
<body>
<input type="button" onclick="DisplayPrompt()" value="JavaScript提示框"/>
</body>
</html>

posted on 2008-07-14 22:48  CodeShark  阅读(2021)  评论(0编辑  收藏  举报

导航