JQUERY dialog的用法詳細(xì)解析
今天用到了客戶端的對(duì)話框,把 jQuery UI 中的對(duì)話框?qū)W習(xí)了一下。
準(zhǔn)備 jQuery 環(huán)境
首先,我們創(chuàng)建一個(gè)按鈕,點(diǎn)擊這個(gè)按鈕的時(shí)候,將會(huì)彈出一個(gè)對(duì)話框。
<input type="button" value="刪除" id="btn" />
為了設(shè)置這個(gè)按鈕點(diǎn)擊的事件,需要準(zhǔn)備 jQuery 的環(huán)境。
<script type="text/javascript" src="scripts/jquery-1.4.2.js"></script>
在 ready 中設(shè)置按鈕的點(diǎn)擊事件。
$(function() {
// 初始化
$("#btn").click(function() {
alert("btn 被點(diǎn)擊啦!");
}
);
確認(rèn)這一步?jīng)]有問(wèn)題。
準(zhǔn)備對(duì)話框
第二步,需要準(zhǔn)備對(duì)話框的內(nèi)容。這些內(nèi)容來(lái)自 jQuery UI 的演示文件。
<div id="dialog-confirm" title="Empty the recycle bin?" >
<p>
<span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>
These items will be permanently deleted and cannot be recovered. Are you sure?</p>
</div>
為了使用 jQuery UI 的對(duì)話框,需要增加這些文件的引用。
<script type="text/javascript" src="scripts/jquery.ui.core.js"></script>
<script type="text/javascript" src="scripts/jquery.ui.widget.js"></script>
<script type="text/javascript" src="scripts/jquery.ui.mouse.js"></script>
<script type="text/javascript" src="scripts/jquery.ui.button.js"></script>
<script type="text/javascript" src="scripts/jquery.ui.draggable.js"></script>
<script type="text/javascript" src="scripts/jquery.ui.position.js"></script>
<script type="text/javascript" src="scripts/jquery.ui.dialog.js"></script>
增加樣式
jQuery UI 中使用了大量的樣式來(lái)修飾,需要引用 jQuery UI 的樣式,注意,jquery.ui.all.css 這個(gè)文件引用了大量的其他樣式文件,將 jQuery UI 中 \development-bundle\themes\base 文件夾中的內(nèi)容都復(fù)制過(guò)來(lái)。
<link type="text/css" href="styles/jquery.ui.all.css" rel="stylesheet" />
在 ready 函數(shù)中,同時(shí)也初始化這個(gè)對(duì)話框。
$(function() {
// 初始化
$("#btn").click(function() {
alert("btn 被點(diǎn)擊啦!");
});
// 初始化對(duì)話框
$("#dialog-confirm").dialog();
});
現(xiàn)在,打開這個(gè)頁(yè)面的時(shí)候,就已經(jīng)可以看到對(duì)話框了。
通過(guò)按鈕彈出對(duì)話框
我們希望頁(yè)面上初始化的時(shí)候看不到這個(gè)對(duì)話框,在點(diǎn)擊按鈕的時(shí)候再出現(xiàn)。那么需要這幾個(gè)工作。
先給對(duì)話框增加一個(gè)默認(rèn)不顯示的樣式。style="display: none",這樣默認(rèn)就不會(huì)看到這一部分。
<div id="dialog-confirm" title="Empty the recycle bin?" style="display: none">
<p>
<span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>
These items will be permanently deleted and cannot be recovered. Are you sure?</p>
</div>
然后,在初始化對(duì)話框的時(shí)候,也不顯示,僅僅完成初始化工作。
在初始化對(duì)話框的時(shí)候,傳遞一個(gè)參數(shù) autoOpen: false
$("#dialog-confirm").dialog(
{
autoOpen: false
}
);
在按鈕的點(diǎn)擊事件中,彈出這個(gè)對(duì)話框。
$("#btn").click(function() {
// alert("btn 被點(diǎn)擊啦!");
$("#dialog-confirm").dialog("open");
});
如果傳遞 close ,將會(huì)關(guān)閉對(duì)話框。
實(shí)現(xiàn)模式對(duì)話框
在實(shí)際應(yīng)用中,我們經(jīng)常需要實(shí)現(xiàn)模式對(duì)話框,在 Web 中需要增加一個(gè)遮罩層來(lái)?yè)踝〉讓拥脑兀M模式效果,這可以在初始化對(duì)話框的時(shí)候,傳遞一個(gè)參數(shù) modal: true 來(lái)實(shí)現(xiàn)。修改之后的初始化代碼成為:
$("#dialog-confirm").dialog(
{
modal: true, // 創(chuàng)建模式對(duì)話框
autoOpen: false, // 只初始化,不顯示
}
);
增加對(duì)話框的按鈕
可以為對(duì)話框增加任意的按鈕,并自定義按鈕的事件處理。我們先增加兩個(gè)按鈕,一個(gè)確定,一個(gè)取消,并讓他們先關(guān)閉對(duì)話框。
// 初始化對(duì)話框
$("#dialog-confirm").dialog(
{
modal: true, // 創(chuàng)建模式對(duì)話框
autoOpen: false,
buttons: {
"Ok": function() {
$(this).dialog('close');
},
"Cancel": function() {
$(this).dialog('close');
}
}
});
- Jquery中dialog屬性小記
- jQuery Dialog 彈出層對(duì)話框插件
- JQuery Dialog(JS 模態(tài)窗口,可拖拽的DIV)
- jQuery UI Dialog 創(chuàng)建友好的彈出對(duì)話框?qū)崿F(xiàn)代碼
- jQuery EasyUI API 中文文檔 - Dialog對(duì)話框
- 淺析JQuery UI Dialog的樣式設(shè)置問(wèn)題
- 用jquery中插件dialog實(shí)現(xiàn)彈框效果實(shí)例代碼
- jquery ui dialog實(shí)現(xiàn)彈窗特效的思路及代碼
- jquery實(shí)現(xiàn)input框獲取焦點(diǎn)的方法
- jquery注冊(cè)文本框獲取焦點(diǎn)清空,失去焦點(diǎn)賦值的簡(jiǎn)單實(shí)例
- jquery獲取焦點(diǎn)和失去焦點(diǎn)事件代碼
- jquery dialog獲取焦點(diǎn)的方法
相關(guān)文章
jQuery使用deferreds串行多個(gè)ajax請(qǐng)求
這篇文章主要介紹了jQuery使用deferreds串行多個(gè)ajax請(qǐng)求的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08jQuery文本框(input textare)事件綁定方法教程
jquery​的事件綁定已經(jīng)用on替換了原來(lái)的bind,接下來(lái)為大家分享下bind的使用方法及input textare事件2013-04-04jquery.jsPlumb實(shí)現(xiàn)拓?fù)鋱D
這篇文章主要為大家詳細(xì)介紹了jquery.jsPlumb實(shí)現(xiàn)拓?fù)鋱D,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03jquery實(shí)現(xiàn)網(wǎng)站列表切換效果的2種方法
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)網(wǎng)站列表切換效果的2種方法,供大家參考,感興趣的小伙伴們可以參考一下2016-08-08jQuery實(shí)現(xiàn)倒計(jì)時(shí)功能 jQuery實(shí)現(xiàn)計(jì)時(shí)器功能
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)倒計(jì)時(shí)功能,jQuery實(shí)現(xiàn)計(jì)時(shí)器功能的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09讓網(wǎng)頁(yè)跳轉(zhuǎn)到指定位置的jquery代碼非書簽
網(wǎng)頁(yè)跳轉(zhuǎn)到指定位置,實(shí)現(xiàn)的方法有很多,本文采用最為簡(jiǎn)單的一種,喜歡朋友可以學(xué)習(xí)下2013-09-09