1. 程式人生 > >jquery自定義對話方塊alert、confirm和prompt

jquery自定義對話方塊alert、confirm和prompt

jQuery Alert Dialogs,又一個基於jQuery的提示框外掛,主要包括Alert、Confirm、prompt這三種,還有一個高階範例,可以在提示框內嵌入HTML語言,可以自定義風格樣式。jQuery的提示框外掛有很多種,每一款都是出自不同的高人之手,因此都比較有自己的特點,包括風格和使用方法等。
效果體驗:http://keleyi.com/keleyi/phtml/jqplug/

英文版:http://keleyi.com/keleyi/phtml/jqplug/1.htm


這個Jquery外掛的目的是替代JavaScript的標準函式alert(),confirm(),和 prompt()。這個外掛有如下這些特點:

1:這個外掛可以使你可以支援你自己的css制定。使你的網站看起來更專業。

2:允許你自定義對話方塊的標題。

3:在IE7中,可以使你避免使用JavaScript 的prompt()函式帶來的頁面重新載入。

4:這些方法都模擬了Windows的模式對話方塊。在你改變改變瀏覽器視窗大小時候,它能夠自適應使用者

視窗的調整。

5:如果你引入了jQuery UI Draggable plugin外掛,那這個外掛也可以被自由拖動。


jquery.alerts.js程式碼:

// Download by http://keleyi.com
// 由 柯樂義 改進改外掛,使外掛適用於新版的jquery(比如1.10.1) 版本

// Visit http://keleyi.com/a/bjac/no0m3cb1.htm for more information
//
// Usage:
//     jAlert( message, [title, callback] )
//     jConfirm( message, [title, callback] )
//     jPrompt( message, [value, title, callback] )
// 
// History:
// // 1.00 - Released (29 December 2008) // 2013-7-8 (function($) { $.alerts = { // These properties can be read/written by accessing $.alerts.propertyName from your scripts at any time verticalOffset: -75, // vertical offset of the dialog from center screen, in pixels horizontalOffset: 0, // horizontal offset of the dialog from center screen, in pixels/
repositionOnResize: true, // re-centers the dialog on window resize overlayOpacity: .01, // transparency level of overlay overlayColor: '#FFF', // base color of overlay draggable: true, // make the dialogs draggable (requires UI Draggables plugin) okButton: '&nbsp;OK&nbsp;', // text for the OK button cancelButton: '&nbsp;Cancel&nbsp;', // text for the Cancel button dialogClass: null, // if specified, this class will be applied to all dialogs // Public methods alert: function(message, title, callback) { if( title == null ) title = 'Alert'; $.alerts._show(title, message, null, 'alert', function(result) { if( callback ) callback(result); }); }, confirm: function(message, title, callback) { if( title == null ) title = 'Confirm'; $.alerts._show(title, message, null, 'confirm', function(result) { if( callback ) callback(result); }); }, prompt: function(message, value, title, callback) { if( title == null ) title = 'Prompt'; $.alerts._show(title, message, value, 'prompt', function(result) { if( callback ) callback(result); }); }, // Private methods _show: function(title, msg, value, type, callback) { $.alerts._hide(); $.alerts._overlay('show'); $("BODY").append( '<div id="popup_container">' + '<h1 id="popup_title"></h1>' + '<div id="popup_content">' + '<div id="popup_message"></div>' + '</div>' + '</div>'); if( $.alerts.dialogClass ) $("#popup_container").addClass($.alerts.dialogClass); // IE6 Fixvar pos = ('undefined' == typeof (document.body.style.maxHeight)) ? 'absolute' : 'fixed'; $("#popup_container").css({ position: pos, zIndex: 99999, padding: 0, margin: 0 }); $("#popup_title").text(title); $("#popup_content").addClass(type); $("#popup_message").text(msg); $("#popup_message").html( $("#popup_message").text().replace(/\n/g, '<br />') ); $("#popup_container").css({ minWidth: $("#popup_container").outerWidth(), maxWidth: $("#popup_container").outerWidth() }); $.alerts._reposition(); $.alerts._maintainPosition(true); switch( type ) { case 'alert': $("#popup_message").after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /></div>'); $("#popup_ok").click( function() { $.alerts._hide(); callback(true); }); $("#popup_ok").focus().keypress( function(e) { if( e.keyCode == 13 || e.keyCode == 27 ) $("#popup_ok").trigger('click'); }); break; case 'confirm': $("#popup_message").after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /> <input type="button" value="' + $.alerts.cancelButton + '" id="popup_cancel" /></div>'); $("#popup_ok").click( function() { $.alerts._hide(); if( callback ) callback(true); }); $("#popup_cancel").click( function() { $.alerts._hide(); if( callback ) callback(false); }); $("#popup_ok").focus(); $("#popup_ok, #popup_cancel").keypress( function(e) { if( e.keyCode == 13 ) $("#popup_ok").trigger('click'); if( e.keyCode == 27 ) $("#popup_cancel").trigger('click'); }); break; case 'prompt': $("#popup_message").append('<br /><input type="text" size="30" id="popup_prompt" />').after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /> <input type="button" value="' + $.alerts.cancelButton + '" id="popup_cancel" /></div>'); $("#popup_prompt").width( $("#popup_message").width() ); $("#popup_ok").click( function() { var val = $("#popup_prompt").val(); $.alerts._hide(); if( callback ) callback( val ); }); $("#popup_cancel").click( function() { $.alerts._hide(); if( callback ) callback( null ); }); $("#popup_prompt, #popup_ok, #popup_cancel").keypress( function(e) { if( e.keyCode == 13 ) $("#popup_ok").trigger('click'); if( e.keyCode == 27 ) $("#popup_cancel").trigger('click'); }); if( value ) $("#popup_prompt").val(value); $("#popup_prompt").focus().select(); break; } // Make draggable if( $.alerts.draggable ) { try { $("#popup_container").draggable({ handle: $("#popup_title") }); $("#popup_title").css({ cursor: 'move' }); } catch(e) { /* requires jQuery UI draggables */ } } }, _hide: function() { $("#popup_container").remove(); $.alerts._overlay('hide'); $.alerts._maintainPosition(false); }, _overlay: function(status) { switch( status ) { case 'show': $.alerts._overlay('hide'); $("BODY").append('<div id="popup_overlay"></div>'); $("#popup_overlay").css({ position: 'absolute', zIndex: 99998, top: '0px', left: '0px', width: '100%', height: $(document).height(), background: $.alerts.overlayColor, opacity: $.alerts.overlayOpacity }); break; case 'hide': $("#popup_overlay").remove(); break; } }, _reposition: function() { var top = (($(window).height() / 2) - ($("#popup_container").outerHeight() / 2)) + $.alerts.verticalOffset; var left = (($(window).width() / 2) - ($("#popup_container").outerWidth() / 2)) + $.alerts.horizontalOffset; if( top < 0 ) top = 0; if( left < 0 ) left = 0; // IE6 fix if ('undefined' == typeof (document.body.style.maxHeight)) top = top + $(window).scrollTop(); $("#popup_container").css({ top: top + 'px', left: left + 'px' }); $("#popup_overlay").height( $(document).height() ); }, _maintainPosition: function(status) { if( $.alerts.repositionOnResize ) { switch(status) { case true: $(window).bind('resize', function() { $.alerts._reposition(); }); break; case false: $(window).unbind('resize'); break; } } } } // Shortuct functions jAlert = function(message, title, callback) { $.alerts.alert(message, title, callback); } jConfirm = function(message, title, callback) { $.alerts.confirm(message, title, callback); }; jPrompt = function(message, value, title, callback) { $.alerts.prompt(message, value, title, callback); }; })(jQuery);

CSS程式碼:

#popup_container {
font-family: Arial, sans-serif;
font-size: 12px;
min-width: 300px; /* Dialog will be no smaller than this */
max-width: 600px; /* Dialog will wrap after this width */
background: #FFF;
border: solid 5px #999;
color: #000;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
}
/* http://keleyi.com 柯樂義*/
#popup_title {
font-size: 14px;
font-weight: bold;
text-align: center;
line-height: 1.75em;
color: #666;
background: #CCC url(images/title.gif) top repeat-x;
border: solid 1px #FFF;
border-bottom: solid 1px #999;
cursor: default;
padding: 0em;
margin: 0em;
}

#popup_content {
background: 16px 16px no-repeat url(http://keleyi.com/keleyi/phtml/jqplug/index/info.gif);
padding: 1em 1.75em;
margin: 0em;
}

#popup_content.alert {
background-image: url(http://keleyi.com/keleyi/phtml/jqplug/index/info.gif);
}

#popup_content.confirm {
background-image: url(http://keleyi.com/keleyi/phtml/jqplug/index/important.gif);
}

#popup_content.prompt {
background-image: url(http://keleyi.com/keleyi/phtml/jqplug/index/help.gif);
}

#popup_message {
padding-left: 48px;
}

#popup_panel {
text-align: center;
margin: 1em 0em 0em 1em;
}

#popup_prompt {
margin: .5em 0em;
}

還需引用:
<script type="text/javascript" src="http://keleyi.com/keleyi/pmedia/jquery-1.9.1.min.js"></script>
<script src="http://keleyi.com/keleyi/pmedia/jquery/ui/1.10.3/js/jquery-ui-1.10.3.min.js" type="text/javascript"></script>

相關推薦

jquery定義對話方塊alertconfirmprompt

jQuery Alert Dialogs,又一個基於jQuery的提示框外掛,主要包括Alert、Confirm、prompt這三種,還有一個高階範例,可以在提示框內嵌入HTML語言,可以自定義風格樣式。jQuery的提示框外掛有很多種,每一款都是出自不同的高人之手,因此都比較有自己的特點,包括風格和使用方法

JavaScript彈出對話方塊alertconfirmprompt

1、alert()–警告訊息框  alert 方法有一個引數,即希望對使用者顯示的文字字串。該字串不是 HTML 格式。該訊息框提供了一個“確定”按鈕讓使用者關閉該訊息框,並且該訊息框是模式對話方塊,  也就是說,使用者必須先關閉該訊息框然後才能繼續進行操作。  例如:

js 三種彈窗 alertconfirmprompt

alert:警告 經常用作程式除錯時彈出結果。在彈出後點擊"確定"按鈕之前,不能進行任何其它操作,所以可以把想要在點選“確定”按鈕之後執行的語句寫到後面就可以了,並沒有對“確定”按鈕有監聽事件 function toast(){ alert("警告"); consol

jquery+css實現定義對話方塊功能(不使用外掛)

當今網路上各種jquery對話方塊外掛層出不窮,可是我為什麼要放棄這些外掛選擇自己使用jquery和css來自定義對話方塊的呢?有兩方面的原因,一個是有利於自己更加深入的瞭解css和jquery的特性,另一方面也可以更加的相容自己的專案。這裡面有幾個關鍵性的技術點,但是我們

定義對話方塊全屏模式(模擬Activity)

記錄一下開發中碰到的自定義對話方塊需要更改為Activity 樣式,全屏, 有輸入框被鍵盤遮擋的解決方式: 1.佈局檔案寫法: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andr

詳解Dialog(三)——定義對話方塊檢視及引數傳遞

前言:這篇文章是有關對話方塊的最後一篇了,最近工作是很忙,不過閒下來的時候也不想寫部落格,估計是累過頭了,還好以前定了個任務,每個月必須寫四篇,這才強制自己去一直更新,馬總說過,夢想這東西還是要有的,萬一實現了呢,趁在阿里的兩年,努力! 相關文章: 今天給大家講講有關自定義對話方塊的相關內容,前面兩篇都

android定義對話方塊去除黑底

在做Android開發中經常會使用到自定義樣式的Dialog,尤其是在遊戲當中,大家通常都是通過自定義一個佈局檔案來設定Dialog中顯示的內容,但是僅僅這樣還是不行的~會有黑色的框和白色的邊。這就需要我們自定義Dialog的樣式了。 首先上圖: 首先是Dialog的

Android 完全定義對話方塊的實現(標題欄+EditText+雙按鈕)

糾結了我一下午,為了能使用我比較鐘意的自定義對話方塊,我可謂絞盡腦汁,這裡寫下來 以表忠心。 這是我開始從網上看到的別人寫的自定義框。博文地址在這:點選 我的目的不僅僅是提示框,我想將其改成可以在中間輸入資料,然後按下確定我還可以獲取其中的資料來用的對話方塊。 然後

安卓中實現定義對話方塊以及定義顯示位置

專案中有用到如下的對話方塊效果,本來是想用popwindow來實現的結果發現不能覆蓋原來的佈局,不知道是我瞭解的不夠還是本身就不行,發現彈出的對話方塊會與原來的佈局重疊 後面就用對話方塊來實現了,接下來講一下如何實現自定義對話方塊和自定義對話方塊的彈出位置

InstallShield建立定義對話方塊 例項2

由於http://blog.csdn.net/dragoo1/article/details/44758243裡的對話方塊比較簡單,沒有edit賦值,取值,操作,這裡多寫一點,先在對話方塊新增一個edit #define DLG_MYDLG "MyDlg"    //MyD

android 如何在定義對話方塊中獲取edittext中的資料

在專案中忽然遇到這樣的問題,需要自定義對話方塊,對話方塊需要有一個輸入框,以便修改所選中的價格,然後點選確定之後,修改所顯示的價格。遇到的最大的問題就是如何能夠獲取到自定義對話方塊當中edittext輸入的數值,百度了很久,看到的答案都是如下: //得到自定義對話方塊

學習筆記之Qt定義對話方塊

1、用qt creator 建立一個名為mydialog的qt empty project工程; 2、在該工程中新增logindlg.cpp、logindlg.h、mydialog.cpp檔案,程式碼如下:logindlg.h //定義巨集變數,確保該標頭檔案只被包含一次

webView顯示H5中的對話方塊或者定義對話方塊或者toast

//只是顯示系統對話方塊就 加下面程式碼webView.setWebChromeClient(new WebChromeClient());//自定義 webView.setWebChromeClient(new WebChromeClient() { @Overri

qt中獲取開啟檔案路徑,顏色對話方塊,字型對話方塊定義對話方塊,訊息對話方塊,輸入對話方塊

1.獲取開啟檔案的檔案路徑 //獲取開啟檔案的檔案路徑--父視窗,視窗名稱,開啟路徑,檔案篩選 QString s = QFileDialog::getOpenFileName(this,"open file dialog","/","C++ files(*.c

react-native modal定義對話方塊

效果如圖: 程式碼: react native版本0.39.0 /** * Created by xq on 16/12/6. */ import React, { Component ,PropTypes} from 'react'; import { T

android封裝框架入門之從定義對話方塊開始callback幫你忙

android封裝框架入門之從封裝確定、取消對話方塊開始 最近,封裝了一個輕量級的網路非同步網路請求框架MHttpUtils(傳送門),由於非常喜歡RxJava和Retrofit等的鏈式程式設計風格,於是自己搗鼓著按這種風格來封裝,期間的一些封裝思路抽取出

C#定義對話方塊用法的感悟

教材:C#程式設計及應用教程  馬駿   人民郵電出版社 參考章節: 第六章例6-4 自定義窗體對話方塊的用法。 (1)新建一個名為DialogExample 的Windows應用程式專案,在【解決方案資源管理器】中將Form1.cs換名為MainForm.cs; (2)新

android中的對話方塊之三:定義對話方塊

首先看下效果圖 下面講一下具體的實現: 1.修改系統預設的Dialog樣式(風格、主題) 2.自定義Dialog佈局檔案 3.可以自己封裝一個類,繼承自Dialog或者直接使用Dialog類來實現,為了方便以後重複使用,建議自己封裝一個Dialog類 ==

android 定義對話方塊

想要自己設計對話方塊的話 1、在xml中設計自己想要的樣式 在style中重寫主題 2、新建一個java檔案繼承dialog 重寫相應的方法 3、在現實的activity中呼叫自定義的對話方塊 演示效果: 下面是演示程式碼: 對話方塊的佈局檔案 xml:<?xml

Android 日常封裝之暴力CustomViewDialog定義對話方塊

import android.app.Dialog; import android.content.Context; import android.support.annotation.LayoutRe