学习ES6(十)-- 对话框

JavaScript支持三种重要类型的对话框--警报对话框,确认对话框,提示对话框。

警报对话框

警报对话框通常用于向用户发送警告消息。例如,如果一个输入字段要求输入一些文本,但用户未提供任何输入,则作为验证的一部分,您可以使用警报框发送警告消息。警报框仅提供一个按钮“确定”以选择并继续。

<html> 
   <head> 
      <script type = "text/javascript"> 
         function Warn() {  
            alert ("This is a warning message!");  
            document.write ("This is a warning message!");  
         } 
      </script> 
   </head> 

   <body> 
      <p>Click the following button to see the result: </p> 
      <form> 
         <input type = "button" value = "Click Me" onclick = "Warn();" /> 
      </form> 
   </body> 
</html> 

确认对话框

确认对话框通常用于征得用户对任何选项的同意。它显示一个带有两个按钮的对话框:“确定”和“取消”。

如果用户单击“确定”按钮,则窗口方法Confirm()将返回true。如果用户单击“取消”按钮,则Confirm()返回false。您可以如下使用确认对话框。

<html> 
   <head> 
      <script type = "text/javascript"> 
         function getConfirmation(){  
            var retVal = confirm("Do you want to continue ?");  
            //判断点击了确认按钮还是取消
            if( retVal == true ){  
               document.write ("User wants to continue!");  
               return true;  
            } else {  
               Document.write ("User does not want to continue!");  
               return false;  
            }  
         }  
      </script> 
   </head> 

   <body> 
      <p>Click the following button to see the result: </p> 
      <form> 
         <input type = "button" value = "Click Me" onclick = "getConfirmation();" /> 
      </form> 
   </body> 
</html> 

提示对话框

当您要弹出文本框以获取用户输入时,提示对话框非常有用。因此,它使您可以与用户进行交互。用户需要填写该字段,然后单击“确定”。

该对话框是使用名为hint()的方法显示的,该方法带有两个参数:(i)您要在文本框中显示的标签,以及(ii)要在文本框中显示的默认字符串。

此对话框有两个按钮:“确定”和“取消”。如果用户单击“确定”按钮,则窗口方法hint()将从文本框中返回输入的值。如果用户单击“取消”按钮,则窗口方法hint()返回null。

<html> 
   <head> 
      <script type = "text/javascript"> 
         function getValue(){  
            var retVal = prompt("Enter your name : ", "your name here");  
            document.write("You have entered : " + retVal);  
         }  
      </script> 
   </head> 

   <body> 
      <p>Click the following button to see the result: </p> 
      <form> 
         <input type = "button" value = "Click Me" onclick = "getValue();" /> 
      </form> 
   </body> 
</html> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值