jquery控件-实现自定义样式的弹出窗口和确认框(转)


- (function () {
- $.MsgBox = {
- Alert: function (title, msg) {
- GenerateHtml("alert", title, msg);
- btnOk(); //alert只是弹出消息,因此没必要用到回调函数callback
- btnNo();
- },
- Confirm: function (title, msg, callback) {
- GenerateHtml("confirm", title, msg);
- btnOk(callback);
- btnNo();
- }
- }
- //生成Html
- var GenerateHtml = function (type, title, msg) {
- var _html = "";
- _html += '<div id="mb_box"></div><div id="mb_con"><span id="mb_tit">' + title + '</span>';
- _html += '<a id="mb_ico">x</a><div id="mb_msg">' + msg + '</div><div id="mb_btnbox">';
- if (type == "alert") {
- _html += '<input id="mb_btn_ok" type="button" value="确定" />';
- }
- if (type == "confirm") {
- _html += '<input id="mb_btn_ok" type="button" value="确定" />';
- _html += '<input id="mb_btn_no" type="button" value="取消" />';
- }
- _html += '</div></div>';
- //必须先将_html添加到body,再设置Css样式
- $("body").append(_html);
- //生成Css
- GenerateCss();
- }
- //生成Css
- var GenerateCss = function () {
- $("#mb_box").css({ width: '100%', height: '100%', zIndex: '99999', position: 'fixed',
- filter: 'Alpha(opacity=60)', backgroundColor: 'black', top: '0', left: '0', opacity: '0.6'
- });
- $("#mb_con").css({ zIndex: '999999', width: '400px', position: 'fixed',
- backgroundColor: 'White', borderRadius: '15px'
- });
- $("#mb_tit").css({ display: 'block', fontSize: '14px', color: '#444', padding: '10px 15px',
- backgroundColor: '#DDD', borderRadius: '15px 15px 0 0',
- borderBottom: '3px solid #009BFE', fontWeight: 'bold'
- });
- $("#mb_msg").css({ padding: '20px', lineHeight: '20px',
- borderBottom: '1px dashed #DDD', fontSize: '13px'
- });
- $("#mb_ico").css({ display: 'block', position: 'absolute', right: '10px', top: '9px',
- border: '1px solid Gray', width: '18px', height: '18px', textAlign: 'center',
- lineHeight: '16px', cursor: 'pointer', borderRadius: '12px', fontFamily: '微软雅黑'
- });
- $("#mb_btnbox").css({ margin: '15px 0 10px 0', textAlign: 'center' });
- $("#mb_btn_ok,#mb_btn_no").css({ width: '85px', height: '30px', color: 'white', border: 'none' });
- $("#mb_btn_ok").css({ backgroundColor: '#168bbb' });
- $("#mb_btn_no").css({ backgroundColor: 'gray', marginLeft: '20px' });
- //右上角关闭按钮hover样式
- $("#mb_ico").hover(function () {
- $(this).css({ backgroundColor: 'Red', color: 'White' });
- }, function () {
- $(this).css({ backgroundColor: '#DDD', color: 'black' });
- });
- var _widht = document.documentElement.clientWidth; //屏幕宽
- var _height = document.documentElement.clientHeight; //屏幕高
- var boxWidth = $("#mb_con").width();
- var boxHeight = $("#mb_con").height();
- //让提示框居中
- $("#mb_con").css({ top: (_height - boxHeight) / 2 + "px", left: (_widht - boxWidth) / 2 + "px" });
- }
- //确定按钮事件
- var btnOk = function (callback) {
- $("#mb_btn_ok").click(function () {
- $("#mb_box,#mb_con").remove();
- if (typeof (callback) == 'function') {
- callback();
- }
- });
- }
- //取消按钮事件
- var btnNo = function () {
- $("#mb_btn_no,#mb_ico").click(function () {
- $("#mb_box,#mb_con").remove();
- });
- }
- })();
Html代码结构如下,js里面拼接的不直观,给出如下:
- <div id="mb_box"></div>
- <div id="mb_con">
- <span id="mb_tit">title</span><a id="mb_ico">x</a>
- <div id="mb_msg">msg</div>
- <div id="mb_btnbox">
- <input id="mb_btn_ok" type="button" value="确定" />
- <input id="mb_btn_no" type="button" value="取消" />
- </div>
- </div>
demo:
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>模拟alert和confirm提示框</title>
- </head>
- <body>
- <input id="add" type="button" value="添加" />
- <input id="delete" type="button" value="删除" />
- <input id="update" type="button" value="修改" />
- <script src="../js/jquery-1.4.1.min.js" type="text/javascript"></script>
- <script src="../js/jquery.similar.msgbox.js" type="text/javascript"></script>
- <script type="text/javascript">
- $("#add").bind("click", function () {
- $.MsgBox.Alert("消息", "哈哈,添加成功!");
- });
- //回调函数可以直接写方法function(){}
- $("#delete").bind("click", function () {
- $.MsgBox.Confirm("温馨提示", "执行删除后将无法恢复,确定继续吗?温馨提示", function () { alert("你居然真的删除了..."); });
- });
- function test() {
- alert("你点击了确定,进行了修改");
- }
- //也可以传方法名 test
- $("#update").bind("click", function () {
- $.MsgBox.Confirm("温馨提示", "确定要进行修改吗?", test);
- });
- //当然你也可以不给回调函数,点击确定后什么也不做,只是关闭弹出层
- //$("#update").bind("click", function () { $.MsgBox.Confirm("温馨提示", "确定要进行修改吗?"); });
- </script>
- </body>
- </html>
jquery控件-实现自定义样式的弹出窗口和确认框(转)的更多相关文章
- WPF 4 DataGrid 控件(自定义样式篇)
原文:WPF 4 DataGrid 控件(自定义样式篇) 在<WPF 4 DataGrid 控件(基本功能篇)>中我们已经学习了DataGrid 的基本功能及使用方法.本篇将继续 ...
- html文件上传控件file自定义样式
问题: HTML自带的file上传按钮因在各种浏览器里显示样式不一.不易自定义样式给我们带来很大的麻烦. 解决思路: 将input[type=file]控件隐藏,使用一个input[type=text ...
- DevExpress第三方控件使用实例之ASPxPopupControl弹出子窗体
弹出页面控件:ASPxPopupControl, <dxpc:ASPxPopupControl ID="popubCtr" runat="server" ...
- C# ie通过打印控件点打印,总是弹出另存为xps的对话框
用的是lodop打印控件,点打印后,总是弹出另存为xps的对话框,后来在网上查到可能是把windows自带的Microsoft XPS Document Writer设为默认打印机的原因. 但现在没有 ...
- 2015.2.16 关于delphi web控件打开新网页时弹出关闭页面(js代码)出错的解决办法研究
参考网址1:http://www.csharpwin.com/csharpspace/2360.shtml...参考网址2:http://www.oschina.net/question/234345 ...
- [js开源组件开发]js手机端浮层控件,并有多种弹出小提示,兼容pc端浏览器
js dialog组件,包含alert和confirm的实现 本组件所有的资源均在github上可以查看源代码 GitHub 本dialog的组件的例子请在这里查看 demo dialog js di ...
- listview点击控件显示EditText,键盘弹出消失的解决方法:
1.软键盘弹出后消失解决方法 AndoridManifet 在activity中添加: android:windowSoftInputMode="adjustPan" 2.使用方式 ...
- WinFrom - DataGridView控件右键选中记录并弹出菜单
dataGridView右键菜单并选中该行 程序代码: private void dataGridView1_CellMouseDown(object sender, DataGridViewCell ...
- 20160513--js 弹出窗口带有iframe控件 备忘
需要引用JQuery. /*! * 主 题:<页面弹出窗口> * 说 明:用于页面弹出的窗口. * 功能描述: * 1.生成弹出窗口,窗口内包括iframe控件: * 2.窗口弹出时,生成 ...
随机推荐
- linux 定时任务---给心爱的小姐姐发情书
目录 1.计划任务基本概述 什么是crond? 为什么要用crond? 2.计划任务时间管理 crontab配置文件解析 crontab的时间编写规则 crontab命令选项 3.计划任务编写实践 使 ...
- How can I check the last time stats was run on Oracle without using OEM
All of the following data dictionary tables have a LAST_ANALYZED column (replace * with USER/ALL/DBA ...
- 小程序内嵌H5页面判断微信及小程序环境
判断微信及小程序环境 1.H5页面引入jweixin-1.3.2.js 2. var ua = window.navigator.userAgent.toLowerCase(); if(ua.matc ...
- QUIC协议学习记录
QUIC(Quick UDP Internet Connections,快速UDP互联网连接)是Google提出的一种基于UDP改进的通信协议,其目的是降低网络通信的延迟,提供更好的用户互动体验. Q ...
- qt学习(三):鼠标图标改变
qt学习 (三):鼠标图标改变 当你进入一个美好的qt软件场景,比如游戏,电脑的黑白图标会让程序逊色不少, 1改图标要加光标的头文件, 2 载入光标图, 3 再设置改光标就可以了 1在头文件中加 #i ...
- PHP chroot() 函数
改变根目录: <?php// Change root directorychroot("/path/to/chroot/"); // Get current director ...
- 彻底理解 JS 中 this 的指向
首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象(这句话有些问题,后面会解释为什么会有问题,虽然 ...
- Linux内核学习--写一个c程序,并在内核中编译,运行
20140506 今天开始学习伟大的开源代表作:Linux内核.之前的工作流于几个简单命令的应用,因着对Android操作系统的情愫,“忍不住”跟随陈利君老师的步伐,开启OS内核之旅.学习路径之一是直 ...
- IO Processing
Types of IO IRP Buffer Management 首先区分一下page的内存与nonpaged的内存,内存如果用页管理,就难免面对被swap out的命运:但是如果用nonpaged ...
- Codeforces Round #499 (Div. 2) Problem-A-Stages(水题纠错)
CF链接 http://codeforces.com/contest/1011/problem/A Natasha is going to fly to Mars. She needs to bui ...