ui中和easy ui中的很像,但是最近用到的時候全然沒有印象,一段時間不用就忘記了,這篇隨筆介紹一下這個控件。
1.實例
官網(wǎng)源代碼中給出了一些實例,首先看看實例是什么樣子的。
a.默認功能
也是最簡單的應用,也就是打開一個對話框,代碼如下
doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Dialog - Default functionalitytitle>
<link rel="stylesheet" href="../../themes/base/all.css">
<script src="../../external/jquery/jquery.js">script>
<script src="../../ui/core.js">script>
<script src="../../ui/widget.js">script>
<script src="../../ui/mouse.js">script>
<script src="../../ui/draggable.js">script>
<script src="../../ui/position.js">script>
<script src="../../ui/resizable.js">script>
<script src="../../ui/button.js">script>
<script src="../../ui/dialog.js">script>
<link rel="stylesheet" href="../demos.css">
<script>
$(function() {
$( "#dialog" ).dialog();
});
script>
head>
<body>
<div id="dialog" title="Basic dialog">
<p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.p>
div>
<div class="demo-description">
<p>The basic dialog window is an overlay positioned within the viewport and is protected from page content (like select elements) shining through with an iframe. It has a title bar and a content area, and can be moved, resized and closed with the 'x' icon by default.p>
div>
body>
html>
引用了一堆的js文件,這個與下載的時候選擇的類型有關模態(tài)對話框是什么時候實例的對話框,可以選擇All UI ,也可以在當前頁面選擇要用到的組件下載。關鍵代碼$( "#" ).();,加載頁面的時候彈出一個對話框。效果如下圖1
圖1
b.模態(tài)對話框
關鍵代碼如下:
$(function() {
$( "#dialog-message" ).dialog({
modal: true,
buttons: {
Ok: function() {
$( this ).dialog( "close" );
}
}
});
});
作用是彈出一個模態(tài)對話框,mode:true,其實就是后面不能回到后面的父頁面,除非關閉當前的對話框。在對話框后面加了一個OK關閉按鈕,效果如下圖2
圖2
c.確認對話框
關鍵代碼如下:
$(function() {
$( "#dialog-confirm" ).dialog({
resizable: false,
height: "auto",
width: 400,
modal: true,
buttons: {
"Delete all items": function() {
//這里可以加入一些操作 $( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
});
加了兩個按鈕,一個確認,一個取消,可以分別做一些操作,其實也不限于這兩個按鈕,如果需要的話可以在下面添加任意多個按鈕,實現(xiàn)任意多的功能。效果如下如3
圖3
帶form功能的對話框
這個例子有些復雜,可以實現(xiàn)驗證,請求等功能,關鍵代碼如下
doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Dialog - Modal formtitle>
<link rel="stylesheet" href="../../themes/base/all.css">
<script src="../../external/jquery/jquery.js">script>
<script src="../../ui/core.js">script>
<script src="../../ui/widget.js">script>
<script src="../../ui/mouse.js">script>
<script src="../../ui/button.js">script>
<script src="../../ui/draggable.js">script>
<script src="../../ui/position.js">script>
<script src="../../ui/resizable.js">script>
<script src="../../ui/dialog.js">script>
<script src="../../ui/effect.js">script>
<link rel="stylesheet" href="../demos.css">
<style>
label, input { display:block; }
input.text { margin-bottom:12px; width:95%; padding: .4em; }
fieldset { padding:0; border:0; margin-top:25px; }
h1 { font-size: 1.2em; margin: .6em 0; }
div#users-contain { width: 350px; margin: 20px 0; }
div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; }
div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; }
.ui-dialog .ui-state-error { padding: .3em; }
.validateTips { border: 1px solid transparent; padding: 0.3em; }
style>
<script>
$(function() {
var dialog, form,
// From http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#e-mail-state-%28type=email%29
emailRegex = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
name = $( "#name" ),
email = $( "#email" ),
password = $( "#password" ),
allFields = $( [] ).add( name ).add( email ).add( password ),
tips = $( ".validateTips" );
function updateTips( t ) {
tips
.text( t )
.addClass( "ui-state-highlight" );
setTimeout(function() {
tips.removeClass( "ui-state-highlight", 1500 );
}, 500 );
}
function checkLength( o, n, min, max ) {
if ( o.val().length > max || o.val().length < min ) {
o.addClass( "ui-state-error" );
updateTips( "Length of " + n + " must be between " +
min + " and " + max + "." );
return false;
} else {
return true;
}
}
function checkRegexp( o, regexp, n ) {
if ( !( regexp.test( o.val() ) ) ) {
o.addClass( "ui-state-error" );
updateTips( n );
return false;
} else {
return true;
}
}
function addUser() {
var valid = true;
allFields.removeClass( "ui-state-error" );
valid = valid && checkLength( name, "username", 3, 16 );
valid = valid && checkLength( email, "email", 6, 80 );
valid = valid && checkLength( password, "password", 5, 16 );
valid = valid && checkRegexp( name, /^[a-z]([0-9a-z_\s])+$/i, "Username may consist of a-z, 0-9, underscores, spaces and must begin with a letter." );
valid = valid && checkRegexp( email, emailRegex, "eg. ui@jquery.com" );
valid = valid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
if ( valid ) {
$( "#users tbody" ).append( "" +
"" + name.val() + " " +
"" + email.val() + " " +
"" + password.val() + " " +
" " );
dialog.dialog( "close" );
}
return valid;
}
dialog = $( "#dialog-form" ).dialog({
autoOpen: false,
height: 400,
width: 350,
modal: true,
buttons: {
"Create an account": addUser,
Cancel: function() {
dialog.dialog( "close" );
}
},
close: function() {
form[ 0 ].reset();
allFields.removeClass( "ui-state-error" );
}
});
form = dialog.find( "form" ).on( "submit", function( event ) {
event.preventDefault();
addUser();
});
$( "#create-user" ).button().on( "click", function() {
dialog.dialog( "open" );
});
});
script>
head>
<body>
<div id="dialog-form" title="Create new user">
<p class="validateTips">All form fields are required.p>
<form>
<fieldset>
<label for="name">Namelabel>
<input type="text" name="name" id="name" value="Jane Smith" class="text ui-widget-content ui-corner-all">
<label for="email">Emaillabel>
<input type="text" name="email" id="email" value="jane@smith.com" class="text ui-widget-content ui-corner-all">
<label for="password">Passwordlabel>
<input type="password" name="password" id="password" value="xxxxxxx" class="text ui-widget-content ui-corner-all">
<input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
fieldset>
form>
div>
<div id="users-contain" class="ui-widget">
<h1>Existing Users:h1>
<table id="users" class="ui-widget ui-widget-content">
<thead>
<tr class="ui-widget-header ">
<th>Nameth>
<th>Emailth>
<th>Passwordth>
tr>
thead>
<tbody>
<tr>
<td>John Doetd>
<td>john.doe@example.comtd>
<td>johndoe1td>
tr>
tbody>
table>
div>
<button id="create-user">Create new userbutton>
<div class="demo-description">
<p>Use a modal dialog to require that the user enter data during a multi-step process. Embed form markup in the content area, set the <code>modalcode> option to true, and specify primary and secondary user actions with the <code>buttonscode> option.p>
div>
body>
html>
這個是帶請求功能的對話框,代碼很嚴謹
var , form,
= /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
name = $( "#name" ),
email = $( "#email" ),
= $( "#" ),
= $( [] ).add( name ).add( email ).add( ),
tips = $( "." );
定義元素變量,要用到的驗證, = $( [] ).add( name ).add( email ).add( ),這個用來把要用到的變量加到集合里面,用來添加和刪除css樣式,后面會用到的,很巧妙,$( [] )這個寫法可以把多個元素添加到一個集合中。
( t ) {
tips
.text( t )
.( "ui-state-" );
(() {
tips.( "ui-state-", 1500 );
}, 500 );
}
給元素添加文字和css樣式,并且在500毫秒內(nèi)移除這個樣式,不明白.( "ui-state-", 1500 )后面一個參數(shù)1500是什么意思
( o, n, min, max ) {
if ( o.val(). > max || o.val(). < min ) {
o.( "ui-state-error" );
( " of " + n + " must be " +
min + " and " + max + "." );
false;
} else {
true;
}
}
檢查輸入文字的長短,4個參數(shù)分別是元素本身,元素名稱,最小長度,最大長度。
( o, , n ) {
if ( !( .test( o.val() ) ) ) {
o.( "ui-state-error" );
( n );
false;
} else {
true;
}
}
用正則表達式檢查元素是否符合規(guī)則模態(tài)對話框是什么時候實例的對話框,3個參數(shù),元素本身,正則表達式,錯誤提示。
() {
var valid = true;
.( "ui-state-error" );
valid = valid && ( name, "", 3, 16 );
valid = valid && ( email, "email", 6, 80 );
valid = valid && ( , "", 5, 16 );
valid = valid && ( name, /^[a-z]([0-9a-z_\s])+$/i, " may of a-z, 0-9, , and must begin with a ." );
valid = valid && ( email, , "eg. " );
valid = valid && ( , /^([0-9a-zA-Z])+$/, " field only allow : a-z 0-9" );
if ( valid ) {
$( "#users tbody" ).( "" +
"
" + name.val() + "
" +
"
" + email.val() + "
" +
"
" + .val() + "
" +
"
" );
.( "close" );
}
valid;
}
添加用戶的方法,驗證方法valid = valid && 這個用的很巧妙,連續(xù)利用了&&操作,這里的寫法非常的靈活也很巧妙。在這里可以實現(xiàn)具體的功能,也可以在form里面添加,form到具體頁面中操作。
= $( "#-form" ).({
: false,
: 400,
width: 350,
modal: true,
: {
" an ": ,
: () {
.( "close" );
}
},
close: () {
form[ 0 ].reset();
.( "ui-state-error" );
}
});
這里才是最主要的功能,一個定義一個對話框,其中有個2個按鈕,一個是添加一個賬號,訪問函數(shù),一個是取消,直接定義關閉對話框,最后還定義了默認的關閉功能,form中清除元素的值,清除錯誤提示。
form = .find( "form" ).on( "", ( event ) {
event.();
();
});
定義form,其實就是頁面中的form,還定義了提交時請求動作,首先取消默認事件,然后執(zhí)行。
$( "#-user" ).().on( "click", () {
.( "open" );
});
給添加用戶按鈕綁定事件,這里綁定事件沒有直接.click,也沒有l(wèi)ive,也沒有,on是官方推薦的一個綁定函數(shù)方法。注意這里不是頁面加載的時候就顯示對話框,而是點擊添加賬號的時候彈出這個對話框,所以要先定義這個對話框“ = $( "#-form" ).({”,點擊添加按鈕的時候彈出對話框“.( "open" );”。
頁面截圖如下圖4
圖4
點擊添加用戶彈出對話框如圖5
圖5
元素驗證失敗時截圖如圖6
圖6
這個例子很經(jīng)典!
d.帶動畫功能的對話框
關鍵代碼如下:
$(function() {
$( "#dialog" ).dialog({
autoOpen: false,
show: {
effect: "blind",
duration: 1000
},
hide: {
effect: "explode",
duration: 1000
}
});
$( "#opener" ).click(function() {
$( "#dialog" ).dialog( "open" );
});
});
定義顯示和影藏效果,: "blind":打開時時百葉窗效果,從上到下顯示,: ""關閉時是爆發(fā),碎片效果。
百葉窗效果如下圖7
圖7
爆炸效果如圖8
圖8
同上,注意這里不是頁面加載的時候就顯示對話框,而是點擊添加賬號的時候彈出這個對話框,所以要先定義這個對話框“$( "#" ).({,”,點擊添加按鈕的時候彈出對話框“.( "open" );”
2.屬性,事件,方法
關于屬性,事件和方法內(nèi)容很多,可以查看中文文檔