对话框(Dialog)


jQuery UI 实例 - 对话框(Dialog)

在一个交互覆盖层中打开内容。

如需了解更多有关 dialog 部件的细节,请查看 API 文档 对话框部件(Dialog Widget)。

默认功能

基本的对话框窗口是一个定位于视区中的覆盖层,同时通过一个 iframe 与页面内容分隔开(就像 select 元素)。它由一个标题栏和一个内容区域组成,且可以移动,调整尺寸,默认可通过 'x' 图标关闭。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 对话框(Dialog) - 默认功能</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <script>
  11.   $(function() {
  12.     $( "#dialog" ).dialog();
  13.   });
  14.   </script>
  15. </head>
  16. <body>
  17.  
  18. <div id="dialog" title="基本的对话框">
  19.   <p>这是一个默认的对话框,用于显示信息。对话框窗口可以移动,调整尺寸,默认可通过 'x' 图标关闭。</p>
  20. </div>
  21.  
  22.  
  23. </body>
  24. </html>

动画

可以通过为 show/hide 属性指定一个特效来动画显示对话框。您必须为想使用的特效引用独立的特效文件。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 对话框(Dialog) - 动画</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <script>
  11.   $(function() {
  12.     $( "#dialog" ).dialog({
  13.       autoOpen: false,
  14.       show: {
  15.         effect: "blind",
  16.         duration: 1000
  17.       },
  18.       hide: {
  19.         effect: "explode",
  20.         duration: 1000
  21.       }
  22.     });
  23.  
  24.     $( "#opener" ).click(function() {
  25.       $( "#dialog" ).dialog( "open" );
  26.     });
  27.   });
  28.   </script>
  29. </head>
  30. <body>
  31.  
  32. <div id="dialog" title="Basic dialog">
  33.   <p>这是一个动画显示的对话框,用于显示信息。对话框窗口可以移动,调整尺寸,默认可通过 'x' 图标关闭。</p>
  34. </div>
  35.  
  36. <button id="opener">打开对话框</button>
  37.  
  38.  
  39. </body>
  40. </html>

基本的模态

模态对话框防止用户与对话框以外的页面其他部分进行交互,直到对话框关闭为止。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 对话框(Dialog) - 基本的模态</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <script>
  11.   $(function() {
  12.     $( "#dialog-modal" ).dialog({
  13.       height: 140,
  14.       modal: true
  15.     });
  16.   });
  17.   </script>
  18. </head>
  19. <body>
  20.  
  21. <div id="dialog-modal" title="基本的模态对话框">
  22.   <p>添加模态覆盖屏幕,让对话框看起来更突出,因为它让页面上其他内容变暗。</p>
  23. </div>
  24.  
  25. <p>Sed vel diam id libero <a href="http://www.shouce.ren">rutrum convallis</a>. Donec aliquet leo vel magna. Phasellus rhoncus faucibus ante. Etiam bibendum, enim faucibus aliquet rhoncus, arcu felis ultricies neque, sit amet auctor elit eros a lectus.</p>
  26.  
  27.  
  28. </body>
  29. </html>

模态确认

确认一个动作可能是破坏性的也可能是有意义的。设置 modal 选项为 true,并通过 buttons 选项来指定主要的和次要的用户动作。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 对话框(Dialog) - 模态确认</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <script>
  11.   $(function() {
  12.     $( "#dialog-confirm" ).dialog({
  13.       resizable: false,
  14.       height:140,
  15.       modal: true,
  16.       buttons: {
  17.         "Delete all items": function() {
  18.           $( this ).dialog( "close" );
  19.         },
  20.         Cancel: function() {
  21.           $( this ).dialog( "close" );
  22.         }
  23.       }
  24.     });
  25.   });
  26.   </script>
  27. </head>
  28. <body>
  29.  
  30. <div id="dialog-confirm" title="清空回收站吗?">
  31.   <p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>这些项目将被永久删除,并且无法恢复。您确定吗?</p>
  32. </div>
  33.  
  34. <p>Sed vel diam id libero <a href="http://www.shouce.ren">rutrum convallis</a>. Donec aliquet leo vel magna. Phasellus rhoncus faucibus ante. Etiam bibendum, enim faucibus aliquet rhoncus, arcu felis ultricies neque, sit amet auctor elit eros a lectus.</p>
  35.  
  36.  
  37. </body>
  38. </html>

模态表单

使用模态对话框来请求用户在一个多步骤过程中输入数据。在内容区域嵌入 form 标记,设置 modal 选项为 true,并通过 buttons 选项来指定主要的和次要的用户动作。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 对话框(Dialog) - 模态表单</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <style>
  11.     body { font-size: 62.5%; }
  12.     label, input { display:block; }
  13.     input.text { margin-bottom:12px; width:95%; padding: .4em; }
  14.     fieldset { padding:0; border:0; margin-top:25px; }
  15.     h1 { font-size: 1.2em; margin: .6em 0; }
  16.     div#users-contain { width: 350px; margin: 20px 0; }
  17.     div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; }
  18.     div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; }
  19.     .ui-dialog .ui-state-error { padding: .3em; }
  20.     .validateTips { border: 1px solid transparent; padding: 0.3em; }
  21.   </style>
  22.   <script>
  23.   $(function() {
  24.     var name = $( "#name" ),
  25.       email = $( "#email" ),
  26.       password = $( "#password" ),
  27.       allFields = $( [] ).add( name ).add( email ).add( password ),
  28.       tips = $( ".validateTips" );
  29.  
  30.     function updateTips( t ) {
  31.       tips
  32.         .text( t )
  33.         .addClass( "ui-state-highlight" );
  34.       setTimeout(function() {
  35.         tips.removeClass( "ui-state-highlight", 1500 );
  36.       }, 500 );
  37.     }
  38.  
  39.     function checkLength( o, n, min, max ) {
  40.       if ( o.val().length > max || o.val().length < min ) {
  41.         o.addClass( "ui-state-error" );
  42.         updateTips( "" + n + " 的长度必须在 " +
  43.           min + " 和 " + max + " 之间。" );
  44.         return false;
  45.       } else {
  46.         return true;
  47.       }
  48.     }
  49.  
  50.     function checkRegexp( o, regexp, n ) {
  51.       if ( !( regexp.test( o.val() ) ) ) {
  52.         o.addClass( "ui-state-error" );
  53.         updateTips( n );
  54.         return false;
  55.       } else {
  56.         return true;
  57.       }
  58.     }
  59.  
  60.     $( "#dialog-form" ).dialog({
  61.       autoOpen: false,
  62.       height: 300,
  63.       width: 350,
  64.       modal: true,
  65.       buttons: {
  66.         "创建一个帐户": function() {
  67.           var bValid = true;
  68.           allFields.removeClass( "ui-state-error" );
  69.  
  70.           bValid = bValid && checkLength( name, "username", 3, 16 );
  71.           bValid = bValid && checkLength( email, "email", 6, 80 );
  72.           bValid = bValid && checkLength( password, "password", 5, 16 );
  73.  
  74.           bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "用户名必须由 a-z、0-9、下划线组成,且必须以字母开头。" );
  75.           // From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
  76.           bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com" );
  77.           bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "密码字段只允许: a-z 0-9" );
  78.  
  79.           if ( bValid ) {
  80.             $( "#users tbody" ).append( "<tr>" +
  81.               "<td>" + name.val() + "</td>" +
  82.               "<td>" + email.val() + "</td>" +
  83.               "<td>" + password.val() + "</td>" +
  84.             "</tr>" );
  85.             $( this ).dialog( "close" );
  86.           }
  87.         },
  88.         Cancel: function() {
  89.           $( this ).dialog( "close" );
  90.         }
  91.       },
  92.       close: function() {
  93.         allFields.val( "" ).removeClass( "ui-state-error" );
  94.       }
  95.     });
  96.  
  97.     $( "#create-user" )
  98.       .button()
  99.       .click(function() {
  100.         $( "#dialog-form" ).dialog( "open" );
  101.       });
  102.   });
  103.   </script>
  104. </head>
  105. <body>
  106.  
  107. <div id="dialog-form" title="创建新用户">
  108.   <p class="validateTips">所有的表单字段都是必填的。</p>
  109.  
  110.   <form>
  111.   <fieldset>
  112.     <label for="name">名字</label>
  113.     <input type="text" name="name" id="name" class="text ui-widget-content ui-corner-all">
  114.     <label for="email">邮箱</label>
  115.     <input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all">
  116.     <label for="password">密码</label>
  117.     <input type="password" name="password" id="password" value="" class="text ui-widget-content ui-corner-all">
  118.   </fieldset>
  119.   </form>
  120. </div>
  121.  
  122.  
  123. <div id="users-contain" class="ui-widget">
  124.   <h1>已有的用户:</h1>
  125.   <table id="users" class="ui-widget ui-widget-content">
  126.     <thead>
  127.       <tr class="ui-widget-header ">
  128.         <th>名字</th>
  129.         <th>邮箱</th>
  130.         <th>密码</th>
  131.       </tr>
  132.     </thead>
  133.     <tbody>
  134.       <tr>
  135.         <td>John Doe</td>
  136.         <td>john.doe@example.com</td>
  137.         <td>johndoe1</td>
  138.       </tr>
  139.     </tbody>
  140.   </table>
  141. </div>
  142. <button id="create-user">创建新用户</button>
  143.  
  144.  
  145. </body>
  146. </html>

模态消息

使用模态对话框来在下一步动作执行之前确认信息和动作。设置 modal 选项为 true,并通过 buttons 选项来指定主要的动作(Ok)。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>jQuery UI 对话框(Dialog) - 模态消息</title>
  6.   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7.   <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8.   <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.   <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10.   <script>
  11.   $(function() {
  12.     $( "#dialog-message" ).dialog({
  13.       modal: true,
  14.       buttons: {
  15.         Ok: function() {
  16.           $( this ).dialog( "close" );
  17.         }
  18.       }
  19.     });
  20.   });
  21.   </script>
  22. </head>
  23. <body>
  24.  
  25. <div id="dialog-message" title="下载完成">
  26.   <p>
  27.     <span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
  28.     您的文件已经成功下载到文件夹中。
  29.   </p>
  30.   <p>
  31.     当前使用存储空间的 <b>36%</b>
  32.   </p>
  33. </div>
  34.  
  35. <p>Sed vel diam id libero <a href="http://www.shouce.ren">rutrum convallis</a>. Donec aliquet leo vel magna. Phasellus rhoncus faucibus ante. Etiam bibendum, enim faucibus aliquet rhoncus, arcu felis ultricies neque, sit amet auctor elit eros a lectus.</p>
  36.  
  37.  
  38. </body>
  39. </html>