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

jQuery UI庫中dialog對話框功能使用全解析_jquery

May 16, 2016 pm 03:04 PM
dialog javascript jquery js 對話框

   對話框(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

熱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

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

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

建議:優秀JS開源人臉偵測辨識項目 建議:優秀JS開源人臉偵測辨識項目 Apr 03, 2024 am 11:55 AM

人臉偵測辨識技術已經是一個比較成熟且應用廣泛的技術。而目前最廣泛的網路應用語言非JS莫屬,在Web前端實現人臉偵測辨識相比後端的人臉辨識有優勢也有弱勢。優點包括減少網路互動、即時識別,大大縮短了使用者等待時間,提高了使用者體驗;弱勢是:受到模型大小限制,其中準確率也有限。如何在web端使用js實現人臉偵測呢?為了實現Web端人臉識別,需要熟悉相關的程式語言和技術,如JavaScript、HTML、CSS、WebRTC等。同時也需要掌握相關的電腦視覺和人工智慧技術。值得注意的是,由於Web端的計

jQuery中如何使用PUT請求方式? jQuery中如何使用PUT請求方式? Feb 28, 2024 pm 03:12 PM

jQuery中如何使用PUT請求方式?在jQuery中,發送PUT請求的方法與發送其他類型的請求類似,但需要注意一些細節和參數設定。 PUT請求通常用於更新資源,例如更新資料庫中的資料或更新伺服器上的檔案。以下是在jQuery中使用PUT請求方式的具體程式碼範例。首先,確保引入了jQuery庫文件,然後可以透過以下方式發送PUT請求:$.ajax({u

jQuery如何移除元素的height屬性? jQuery如何移除元素的height屬性? Feb 28, 2024 am 08:39 AM

jQuery如何移除元素的height屬性?在前端開發中,經常會遇到需要操作元素的高度屬性的需求。有時候,我們可能需要動態改變元素的高度,而有時候又需要移除元素的高度屬性。本文將介紹如何使用jQuery來移除元素的高度屬性,並提供具體的程式碼範例。在使用jQuery操作高度屬性之前,我們首先需要了解CSS中的height屬性。 height屬性用於設定元素的高度

jQuery小技巧:快速修改頁面所有a標籤的文本 jQuery小技巧:快速修改頁面所有a標籤的文本 Feb 28, 2024 pm 09:06 PM

標題:jQuery小技巧:快速修改頁面所有a標籤的文字在網頁開發中,我們經常需要對頁面中的元素進行修改和操作。使用jQuery時,有時候需要一次修改頁面中所有a標籤的文字內容,這樣可以節省時間和精力。以下將介紹如何使用jQuery快速修改頁面所有a標籤的文本,同時給出具體的程式碼範例。首先,我們需要引入jQuery庫文件,確保在頁面中引入了以下程式碼:&lt

js和vue的關係 js和vue的關係 Mar 11, 2024 pm 05:21 PM

js和vue的關係:1、JS作為Web開發基石;2、Vue.js作為前端框架的崛起;3、JS與Vue的互補關係;4、JS與Vue的實踐應用。

使用jQuery修改所有a標籤的文字內容 使用jQuery修改所有a標籤的文字內容 Feb 28, 2024 pm 05:42 PM

標題:使用jQuery修改所有a標籤的文字內容jQuery是一款受歡迎的JavaScript庫,被廣泛用於處理DOM操作。在網頁開發中,經常會遇到需要修改頁面上連結標籤(a標籤)的文字內容的需求。本文將介紹如何使用jQuery來實現這個目標,並提供具體的程式碼範例。首先,我們需要在頁面中引入jQuery庫。在HTML檔案中加入以下程式碼:

如何判斷jQuery元素是否具有特定屬性? 如何判斷jQuery元素是否具有特定屬性? Feb 29, 2024 am 09:03 AM

如何判斷jQuery元素是否具有特定屬性?在使用jQuery操作DOM元素時,常會遇到需要判斷元素是否具有某個特定屬性的情況。在這種情況下,我們可以藉助jQuery提供的方法來輕鬆實現這項功能。以下將介紹兩種常用的方法來判斷一個jQuery元素是否具有特定屬性,並附上具體的程式碼範例。方法一:使用attr()方法和typeof運算子//判斷元素是否具有特定屬

了解jQuery中eq的作用及應用場景 了解jQuery中eq的作用及應用場景 Feb 28, 2024 pm 01:15 PM

jQuery是一種流行的JavaScript庫,被廣泛用於處理網頁中的DOM操作和事件處理。在jQuery中,eq()方法是用來選擇指定索引位置的元素的方法,具體使用方法和應用場景如下。在jQuery中,eq()方法選擇指定索引位置的元素。索引位置從0開始計數,即第一個元素的索引是0,第二個元素的索引是1,依此類推。 eq()方法的語法如下:$("s

See all articles