搜尋
首頁web前端js教程jQuery UI庫中dialog對話框功能使用全解析_jquery

   對話框(dialog),是jQuery UI 非常重要的功能。它徹底的取代了JavaScript
的alert()、prompt()等方法,也避免了新視窗或頁面的繁雜冗餘。

一.開啟多個dialog
   只要設定不同的id 即可實現。

$('#x').dialog(); 
$('#y').dialog(); 

二.修改dialog 樣式
   在彈出的dialog 對話框中,在火狐瀏覽器中開啟Firebug 或右鍵->檢視
元素。可以看看dialog 的樣式,對dialog 的標題背景進行修改。

//无须修改ui 里的CSS,直接用style.css 替代掉 
.ui-widget-header { 
 
  background:url(../img/xxx.png); 
 
} 

三. dialog()方法的屬性
   對話方塊方法有兩種形式:1.dialog(options),options 是以物件鍵值對的形式
傳參,每個鍵值對錶示一個選項;2.dialog('action', param),action 是操作對
話框方法的字串,param則是options 的某個選項。

dialog 外觀選項
屬性
dialog 外观选项
属性
默认值/类型
说明
title
无/字符串
对话框的标题,可以直接设置在DOM 元素上
buttons
无/对象
以对象键值对方式,给dialog 添加按钮。键是按钮
的名称,值是用户点击后调用的回调函数
預設值/類型
說明
title
無/字串
$('#reg').dialog({ 
 
 title : '注册', 
 
 buttons : { 
 
   '按钮' : function () {} 
 
 } 
 
); 
對話框的標題,可以直接設定在DOM 元素上
buttons

無/物件
dialog 页面位置选项
属性
默认值/类型
说明
position
center/字符串
设置一个对话框窗口的坐标位置,默认为center。
其他设置值为:left top、top right、bottom left、
right bottom(四个角)、top、bottom(顶部或底
部,宽度居中)、left 或right(左边或右边,高度
居中)、center(默认值)
以物件鍵值對方式,為dialog 新增按鈕。鍵是按鈕
的名稱,值是使用者點擊後呼叫的回呼函數
dialog 頁面位置選項
屬性 預設值/類型 說明
position center/字串
設定一個對話框視窗的座標位置,預設為center。
其他設定值為:left top、top right、bottom left、
right bottom(四個角落)、top、bottom(頂部或底
部,寬度居中)、left 或right(左邊或右邊,高度
居中)、center(預設值)

$('#reg').dialog({ 
 
  position : 'left top' 
 
}); 

dialog 大小选项
属性
默认值/类型
说明
width
300/数值
对话框的宽度。默认为300,单位是像素。
height 
auto/数值
对话框的高度。默认为auto,单位是像素。
minWidth 
150/数值
对话框的最小宽度。默认150,单位是像素。
minHeight 
150/数值
对话框的最小高度。默认150,单位是像素。
maxWidth 
auto/数值
对话框的最大宽度。默认auto,单位是像素。
maxHeight 
auto/数值
对话框的最大高度。默认auto,单位是像素。

$('#reg').dialog({ 
 
  height : 500, 
 
  width : 500, 
 
  minWidth : 300, 
 
  minHeight : 300, 
 
  maxWidth : 800, 
 
  maxHeight : 600 
 
}); 

dialog 视觉选项
属性
默认值/类型
说明
show 
false/布尔值
显示对话框时,默认采用淡入效果。
hide 
false 布尔值
关闭对话框时,默认采用淡出效果。

$('#reg').dialog({ 
 
  show : true, 
 
  hide : true 
 
}); 

注意:设置true 后,默认为淡入淡出,如果想使用别的特效,可以使用以下表格中的字符串参数。

show 和hide 可选特效
特效名称
说明
blind 
对话框从顶部显示或消失
bounce 
对话框断断续续地显示或消失,垂直运动
clip 
对话框从中心垂直地显示或消失
slide 
对话框从左边显示或消失
drop 
对话框从左边显示或消失,有透明度变化
fold 
对话框从左上角显示或消失
highlight 
对话框显示或消失,伴随着透明度和背景色的变化
puff 
对话框从中心开始缩放。显示时“收缩”,消失时“生长”
scale 
对话框从中心开始缩放。显示时“生长”,消失时“收缩”
pulsate 
对话框以闪烁形式显示或消失

$('#reg').dialog({ 
 
  show : 'blind', 
 
  hide : 'blind' 
 
}); 

dialog 行为选项
属性
默认值/类型
说明
autoOpen 
true/布尔值
默认为true,调用dialog()方法时就会打开对话框;
如果为false,对话框不可见,但对话框已创建,可
以通过dialog('open')才能可见。
draggable 
true/布尔值
默认为true,可以移动对话框,false 无法移动。
resizable 
true/布尔值
默认为true,可以调整对话框大小,false 无法调整
modal 
false/布尔值
默认为false,对话框外可操作,true 对话框会遮罩
一层灰纱,无法操作。
closeText
无/字符串
设置关闭按钮的title 文字

$('#reg').dialog({ 
 
  autoOpen : false, 
 
  draggable : false, 
 
  resizable : false, 
 
  modal : true, 
 
  closeText : '关闭' 
 
}); 

四.dialog()方法的事件
   除了属性设置外,dialog()方法也提供了大量的事件。这些事件可以给各种不同状态
时提供回调函数。这些回调函数中的this 值等于对话框内容的div 对象,不是整个对话框
的div。

dialog 事件选项
事件名
说明
focus
当对话框被激活时(首次显示以及每次在上面点击)会
调用focus 方法,该方法有两个参数(event, ui)。此事件中
的ui 参数为空。
create
当对话框被创建时会调用create 方法,该方法有两个参
数(event, ui)。此事件中的ui 参数为空。
open
当对话框被显示时(首次显示或调用dialog('open')方法)
会调用open 方法,该方法有两个参数(event, ui)。此事件
中的ui 参数为空。
beforeClose
当对话框将要关闭时( 当单击关闭按钮或调用
dialog('close')方法),会调用beforeclose 方法。如果该函
数返回false,对话框将不会被关闭。关闭的对话框可以
用dialog('open')重新打开。该方法有两个参数(event, ui)。
此事件中的ui 参数为空。
close
当对话框将要关闭时( 当单击关闭按钮或调用
dialog('close')方法),会调用close 方法。关闭的对话框可
以用dialog('open')重新打开。该方法有两个参数(event,
ui)。此事件中的ui 参数为空。
drag
当对话框移动时,每次移动一点均会调用drag 方法。该
方法有两个参数。该方法有两个参数(event, ui)。此事件
中的ui 有两个属性对象:
1.position,得到当前移动的坐标,有两个子属性:top 和
left。
2.offset,得到当前移动的坐标,有两个子属性:top 和left。
dragStart
当开始移动对话框时,会调用dragStart 方法。该方法有
两个参数(event, ui)。此事件中的ui 有两个属性对象:
1.position, get the current moving coordinates, has two sub-properties: top and
left.
2.offset, get the current moving coordinates, has two sub-properties: top and left.
dragStop
When the dialog box starts to be moved, the dragStop method is called. This method has
Two parameters (event, ui). The ui in this event has two attribute objects:
1.position, get the current moving coordinates, has two sub-properties: top and
left.
2.offset, get the current moving coordinates, has two sub-properties: top and left.
resize
When the dialog box is increased in size, resize
will be called every time you drag it.
Method. This method has two parameters (event, ui). There are four ui in this event
attribute objects:
1.size, gets the size of the dialog box, has two sub-properties: width and
height.
2.position, get the coordinates of the dialog box, has two sub-properties: top and left.
3.originalSize, get the original size of the dialog box, has two sub-properties:
width and height.
4.originalPosition, get the original coordinates of the dialog box, there are two sub-attributes
Sex: top and left.
resizeStart
When starting to drag the dialog box, the resizeStart method will be called. This method has
Two parameters (event, ui). The ui in this event has four attribute objects:
1.size, gets the size of the dialog box, has two sub-properties: width and
height.
2.position, get the coordinates of the dialog box, has two sub-properties: top and left.
3.originalSize, get the original size of the dialog box, has two sub-properties:
width and height.
4.originalPosition, get the original coordinates of the dialog box, there are two sub-attributes
Sex: top and left.
resizeStop
When the drag dialog box is ended, the resizeStart method will be called. This method has
Two parameters (event, ui). The ui in this event has four attribute objects:
1.size, gets the size of the dialog box, has two sub-properties: width and
height.
2.position, get the coordinates of the dialog box, has two sub-properties: top and left.
3.originalSize, get the original size of the dialog box, has two sub-properties:
width and height.
4.originalPosition, get the original coordinates of the dialog box, there are two sub-attributes
Sex: top and left.

//当对话框获得焦点后 
$('#reg').dialog({ 
 
  focus : function (e, ui) { 
 
   alert('获得焦点'); 
 
  } 
 
}); 
 
//当创建对话框时 
$('#reg').dialog({ 
 
  create : function (e, ui) { 
 
   alert('创建对话框'); 
 
  } 
 
}); 
 
//当将要关闭时 
$('#reg').dialog({ 
 
  beforeClose : function (e, ui) { 
 
   alert('关闭前做的事!'); 
 
   return flag; 
 
  } 
 
}); 
 
//关闭对话框时 
$('#reg').dialog({ 
 
  close : function (e, ui) { 
 
   alert('关闭!'); 
 
  } 
 
}); 
 
//对话框移动时 
$('#reg').dialog({ 
 
  drag : function (e, ui) { 
 
   alert('top:' + ui.position.top + '\n' 
 
        + 'left:' + ui.position.left); 
 
  } 
 
}); 
 
//对话框开始移动时 
$('#reg').dialog({ 
 
  dragStart : function (e, ui) { 
 
   alert('top:' + ui.position.top + '\n' 
 
        + 'left:' + ui.position.left); 
 
  } 
 
}); 
 
//对话框结束移动时 
$('#reg').dialog({ 
 
  dragStop : function (e, ui) { 
 
   alert('top:' + ui.position.top + '\n' 
 
        + 'left:' + ui.position.left); 
 
  } 
 
}); 
 
//调整对话框大小时 
$('#reg').dialog({ 
 
  resize : function (e, ui) { 
 
   alert('size:' + ui.size.width + '\n' 
 
        + 'originalSize:' + ui.originalSize.width); 
 
  } 
 
}); 
 
//开始调整对话框大小时 
$('#reg').dialog({ 
 
  resizeStart : function (e, ui) { 
 
   alert('size:' + ui.size.width + '\n' 
 
        + 'originalSize:' + ui.originalSize.width); 
 
  } 
 
}); 
 
//结束调整对话框大小时 
$('#reg').dialog({ 
 
  resizeStop : function (e, ui) { 
 
   alert('size:' + ui.size.width + '\n' 
 
        + 'originalSize:' + ui.originalSize.width); 
 
  } 
 
}); 

dialog('action', param)方法
方法
返回值
说明
dialog('open')
 jQuery 对象
打开对话框
dialog('close')
jQuery 对象
关闭对话框
dialog('destroy')
 jQuery 对象
删除对话框,直接阻断了dialog
dialog('isOpen') 
布尔值
判断对话框是否打开状态
dialog('widget') 
jQuery 对象
获取对话框的jQuery 对象
dialog('option', param) 
一般值
获取options 属性的值
dialog('option', param, value)
jQuery 对象
设置options 属性的值

//初始隐藏对话框 
$('#reg').dialog({ 
 
  autoOpen : false 
 
}); 
 
//打开对话框 
$('#reg_a').click(function () { 
 
  $('#reg').dialog('open'); 
 
}); 
 
//关闭对话框 
$('#reg').click(function () { 
 
  $('#reg').dialog('close'); 
 
}); 
 
//判断对话框打开或关闭状态 
$(document).click(function () { 
 
  alert($('#reg').dialog('isOpen')); 
 
}); 
 
//将指定对话框置前 
$(document).click(function () { 
 
  $('#reg').dialog('moveToTop'); 
 
}); 
 
//获取某个options 的param 选项的值 
var title = $('#reg').dialog('option', 'title'); 
 
alert(title); 
 
 
//设置某个options 的param 选项的值 
$('#reg').dialog('option', 'title', '注册'); 

五.dialog 中使用on()
   在dialog 的事件中,提供了使用on()方法处理的事件方法。

on()方法触发的对话框事件
特效名称
说明
dialogfocus 
得到焦点时触发
dialogopen 
显示时触发
dialogbeforeclose 
将要关闭时触发
dialogclose 
关闭时触发
dialogdrag 
每一次移动时触发
dialogdragstart
开始移动时触发
dialogdragstop 
移动结束后触发
dialogresize 
每次调整大小时触发
dialogresizestart 
开始调整大小时触发
dialogresizestop 
结束调整大小时触发

$('#reg').on('dialogclose', function () { 
 
  alert('关闭'); 
 
}); 

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
C和JavaScript:連接解釋C和JavaScript:連接解釋Apr 23, 2025 am 12:07 AM

C 和JavaScript通過WebAssembly實現互操作性。 1)C 代碼編譯成WebAssembly模塊,引入到JavaScript環境中,增強計算能力。 2)在遊戲開發中,C 處理物理引擎和圖形渲染,JavaScript負責遊戲邏輯和用戶界面。

從網站到應用程序:JavaScript的不同應用從網站到應用程序:JavaScript的不同應用Apr 22, 2025 am 12:02 AM

JavaScript在網站、移動應用、桌面應用和服務器端編程中均有廣泛應用。 1)在網站開發中,JavaScript與HTML、CSS一起操作DOM,實現動態效果,並支持如jQuery、React等框架。 2)通過ReactNative和Ionic,JavaScript用於開發跨平台移動應用。 3)Electron框架使JavaScript能構建桌面應用。 4)Node.js讓JavaScript在服務器端運行,支持高並發請求。

Python vs. JavaScript:比較用例和應用程序Python vs. JavaScript:比較用例和應用程序Apr 21, 2025 am 12:01 AM

Python更適合數據科學和自動化,JavaScript更適合前端和全棧開發。 1.Python在數據科學和機器學習中表現出色,使用NumPy、Pandas等庫進行數據處理和建模。 2.Python在自動化和腳本編寫方面簡潔高效。 3.JavaScript在前端開發中不可或缺,用於構建動態網頁和單頁面應用。 4.JavaScript通過Node.js在後端開發中發揮作用,支持全棧開發。

C/C在JavaScript口譯員和編譯器中的作用C/C在JavaScript口譯員和編譯器中的作用Apr 20, 2025 am 12:01 AM

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。1)C 用于解析JavaScript源码并生成抽象语法树。2)C 负责生成和执行字节码。3)C 实现JIT编译器,在运行时优化和编译热点代码,显著提高JavaScript的执行效率。

JavaScript在行動中:現實世界中的示例和項目JavaScript在行動中:現實世界中的示例和項目Apr 19, 2025 am 12:13 AM

JavaScript在現實世界中的應用包括前端和後端開發。 1)通過構建TODO列表應用展示前端應用,涉及DOM操作和事件處理。 2)通過Node.js和Express構建RESTfulAPI展示後端應用。

JavaScript和Web:核心功能和用例JavaScript和Web:核心功能和用例Apr 18, 2025 am 12:19 AM

JavaScript在Web開發中的主要用途包括客戶端交互、表單驗證和異步通信。 1)通過DOM操作實現動態內容更新和用戶交互;2)在用戶提交數據前進行客戶端驗證,提高用戶體驗;3)通過AJAX技術實現與服務器的無刷新通信。

了解JavaScript引擎:實施詳細信息了解JavaScript引擎:實施詳細信息Apr 17, 2025 am 12:05 AM

理解JavaScript引擎內部工作原理對開發者重要,因為它能幫助編寫更高效的代碼並理解性能瓶頸和優化策略。 1)引擎的工作流程包括解析、編譯和執行三個階段;2)執行過程中,引擎會進行動態優化,如內聯緩存和隱藏類;3)最佳實踐包括避免全局變量、優化循環、使用const和let,以及避免過度使用閉包。

Python vs. JavaScript:學習曲線和易用性Python vs. JavaScript:學習曲線和易用性Apr 16, 2025 am 12:12 AM

Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),