Alert

优质
小牛编辑
122浏览
2023-12-01

描述 (Description)

您可以使用以下方法显示警报模式 -

myApp.alert(text, [title, callbackOk])

OR

myApp.alert(text, [callbackOk]) 

以上方法接受参数,如下所列 -

  • text - 显示带有文本的警报。

  • title - 这是一个显示带标题的警报的可选方法。

  • callbackOk - 这是一个可选方法,它提供回调函数,当用户在警报模式下单击“确定”按钮时执行该函数。

例子 (Example)

以下示例演示了Framework7中警报模式的使用,它在您单击链接时显示警告框 -

<!DOCTYPE html>
<html>
   <head>
      <meta name = "viewport" content = "width = device-width, initial-scale = 1, 
         maximum-scale = 1, minimum-scale = 1, user-scalable = no, minimal-ui" />
      <meta name = "apple-mobile-web-app-capable" content = "yes" />
      <meta name = "apple-mobile-web-app-status-bar-style" content = "black" />
      <title>Alert Modal</title>
      <link rel = "stylesheet" 
         href = "https://cdnjs.cloudflare.com/ajax/libs/framework7/1.4.2/css/framework7.ios.min.css" />
      <link rel = "stylesheet" 
         href = "https://cdnjs.cloudflare.com/ajax/libs/framework7/1.4.2/css/framework7.ios.colors.min.css" />
   </head>
   <body>
      <div class = "views">
         <div class = "view view-main">
            <div class = "navbar">
               <div class = "navbar-inner">
                  <div class = "center sliding">Alert Modal</div>
               </div>
            </div>
            <div class = "pages">
               <div data-page = "index" class = "page navbar-fixed">
                  <div class = "page-content">
                     <div class = "content-block">
                        <p><a href = "#" class = "alert-text">Displays Alert Modal with Text</a></p>
                        <p><a href = "#" class = "alert-text-title">Displays Alert Modal With Text and Title</a></p>
                        <p><a href = "#" class = "alert-text-title-callback">Displays Alert Modal With Text and Title and Callback</a></p>
                        <p><a href = "#" class = "alert-text-callback">Displays Alert Modal With Text and Callback</a></p>
                     </div>
                  </div>
               </div>
            </div>
         </div>
      </div>
      <script type = "text/javascript" 
         src = "https://cdnjs.cloudflare.com/ajax/libs/framework7/1.4.2/js/framework7.min.js"></script>
      <script>
         // Here you can initialize the app
         var myApp = new Framework7();
         // If your using custom DOM library, then save it to $$ variable
         var $$ = Dom7;
         // Add the view
         var mainView = myApp.addView('.view-main', {
            // enable the dynamic navbar for this view:
            dynamicNavbar: true
         });
         $$('.alert-text').on('click', function () {
            myApp.alert('This is alert text!!!');
         });
         $$('.alert-text-title').on('click', function () {
            myApp.alert('This is alert text!!!', 'My Title!');
         });
         $$('.alert-text-title-callback').on('click', function () {
            myApp.alert('This is alert text!!!', 'My Title!', function () {
               myApp.alert('You have clicked the button!!!')
            });
         });
         $$('.alert-text-callback').on('click', function () {
            myApp.alert('This is alert text!!!', function () {
               myApp.alert('You have clicked the button!!!')
            });
         });
      </script>
   </body>
</html>

输出 (Output)

让我们执行以下步骤,看看上面给出的代码是如何工作的 -

  • 将上面给出的HTML代码保存为服务器根文件夹中的modal_alert.html文件。

  • 以http://localhost/modal_alert.html打开此HTML文件,输出显示如下。

  • 当用户单击第一个选项时,它会显示带有文本的警报模式。

  • 当用户单击第二个选项时,它会显示警报模式以及文本和标题。

  • 当用户单击第三个选项时,它会显示带有文本和标题的警报模式,单击“确定”后,它将执行回调函数。

  • 当用户单击最后一个选项时,它会显示带文本的警报模式,单击确定后,它会执行回调函数。