我们在调用系统的Alert,prompt的弹出提示时,不同的系统会有不同的提示框,视觉效果不统一,而且不好看,功能单一,现在我们通过Jquery模拟Alert,prompt,现实统一视觉效果,而且内容丰富的弹出提示。

Jquery可以扩展自己的功能,如果对Jquery开发插件不熟悉的人可以到官方网去看看文档,比较简单易懂。

  1. /*
  2. *  本插件基于JQUERY
  3. *  Jquery版本: 1.7.2
  4. *        Date:2012-06-28
  5. *      Author:Kingwell
  6. *      E-mail:jinhua.leng##gmail.com
  7. *
  8. *  ---------- 接口说明: ----------
  9. *
  10. *  --本插件采用kw命名空间,给Jquery添加静态方法,故调用方法为  $.kw.方法名 参数如下:
  11. *  --调用方法:
  12. *  --alert  $.kw.alert(content,title,callBack)
  13. *  --prompt $.kw.prompt(title,content,callBack)
  14. *
  15. *
  16. *  --   title 标题名称 如果缺省,则为默认标题,如:"系统提示"
  17. *  -- content 内容显示的内容
  18. *  --callback 回调函数,单击确定后要执行的内容
  19. *
  20. *
  21. *  --功能:类似系统功能,支持拖动,响应键盘事件,ESC退出,Enter确定,以及回调函数功能。
  22. *
  23. *
  24. */
  25. $(function () {
  26. $.kw = {
  27. title      : "System information", //默认标题 可修改
  28. speed      : 400, //默认速度 可修改
  29. buttonName : "OK", //确定按钮默认名称 可修改
  30. cancel     : "Cancel", //取消按钮默认名称 可修改
  31. content    : "Content",
  32. //移除遮盖层
  33. del : function () {
  34. $("#alert-layer").remove();
  35. },
  36. //响应ESC键盘退出
  37. esc : function () {
  38. $(document).keyup(function (event) {
  39. if (event.which == 27) {
  40. $.kw.del();
  41. }
  42. });
  43. },
  44. //内容显示功能
  45. alert : function (sContent, sTitle, callBack) {
  46. var title = sTitle || this.title;
  47. var layer = "<div id='alert-layer'><div id='alert-container'><div class='alert-top'></div><div class='alert-box'><div id='alert-title'>" + title + "<div id='alert-close' title='Close'></div></div><div id='alert-content'>" + sContent + "</div><div class='alert-button'><button id='alert-button'>" + this.buttonName + "</button></div></div><div class='alert-bottom'></div></div></div>";
  48. $(layer).fadeIn(this.speed).appendTo("body");
  49. this.setting();
  50. this.move();
  51. $("#alert-button").focus();
  52. $("#alert-close").bind("click", this.del); //移除层
  53. $("#alert-button").bind("click", function () {
  54. if (callBack) {
  55. callBack();
  56. }
  57. $.kw.del();
  58. }); //移除层
  59. this.esc();
  60. },
  61. //提示
  62. confirm : function (sContent, callBack, sTitle) {
  63. var title = sTitle || this.title;
  64. var content = sContent || this.content;
  65. var layer = "<div id='alert-layer'><div id='alert-container'><div class='alert-top'></div><div class='alert-box'><div id='alert-title'>" + title + "<div id='alert-close' title='Close'></div></div><div id='alert-content'>" + sContent + "</div><div class='alert-button'><button id='alert-button'>" + this.buttonName + "</button><button id='alert-cancel'>" + this.cancel + "</button></div></div><div class='alert-bottom'></div></div></div>";
  66. $(layer).fadeIn(this.speed).appendTo("body");
  67. this.setting();
  68. $("#alert-button").focus(); //获得焦点
  69. this.move(); //拖动
  70. $("#alert-close").bind("click", this.del); //移除层
  71. $("#alert-cancel").bind("click", this.del); //移除层
  72. $("#alert-button").bind("click", function () {
  73. $.kw.del();
  74. if (callBack) {
  75. callBack();
  76. };
  77. }); //移除层
  78. this.esc();
  79. },
  80. //框拖动功能
  81. move : function () {
  82. $("#alert-title").mousedown(function (event) {
  83. var l = parseInt($("#alert-container").css("left")),
  84. t = parseInt($("#alert-container").css("top"));
  85. x = event.pageX - l;
  86. y = event.pageY - t;
  87. $("body").bind("mousemove", function (event) {
  88. $("#alert-container").css({
  89. "left" : (event.pageX - x)
  90. });
  91. $("#alert-container").css({
  92. "top" : (event.pageY - y)
  93. });
  94. //$("#alert-container").fadeTo(0,0.9)
  95. });
  96. });
  97. $("body").mouseup(function () {
  98. $("body").unbind("mousemove");
  99. //$("#alert-container").fadeTo(0,1)
  100. });
  101. },
  102. //设置背景层与内位置
  103. setting : function () {
  104. var bcw = document.documentElement.clientWidth,
  105. bch = document.documentElement.clientHeight,
  106. bsh = document.documentElement.scrollHeight,
  107. aw  = $("#alert-container").width() / 2,
  108. ah  = $("#alert-container").height() / 2;
  109. $("#alert-layer").css("height", bsh);
  110. $("#alert-container").css({
  111. "top"  : bch / 2 - ah,
  112. "left" : bcw / 2 - aw
  113. });
  114. }
  115. //$.kw  End
  116. };
  117. });
  1. #alert-layer button:focus{border:1px solid #AAA!important; background:#789!important; color:white; outline:none}
  2. #alert-layer{position:absolute;left:0;top:0;width:100%;height:100%;color:#333;line-height:1;z-index:10000; background:rgba(0,0,0,0.1)}
  3. #alert-layer #alert-container{border-radius:3px; padding:10px; width:390px; position:fixed; _position:absolute;}
  4. #alert-layer .alert-top{background:url(images/conner2.png) no-repeat left top; height:10px;}
  5. #alert-layer .alert-bottom{background:url(images/conner2.png) no-repeat left bottom; height:10px;}
  6. #alert-layer #alert-title{font-size:15px; height:30px;line-height:25px;padding:0 10px;position:relative;font-weight:bold;cursor:move;}
  7. #alert-layer #alert-close{background: url(images/close.gif) no-repeat center center; width:25px; height:25px; position:absolute; cursor:pointer; right:2px; top:0px;}
  8. #alert-layer .alert-button{ padding:5px 10px; text-align:right}
  9. #alert-layer #alert-content{background:#F1F1F1; border-top:1px solid #B9B9B9; border-bottom:1px solid #B9B9B9; padding:10px 15px;}
  10. .alert-box{background:url(images/tc_bg.png) repeat-y left top; padding:0 6px}
  11. #alert-layer button{padding:5px; border:1px solid #CCC; margin:auto 5px; border-radius:2px; min-width:60px;}
  12. #alert-layer h1,#alert-layer h2,#alert-layer h3,#alert-layer h4{margin:10px auto; font-size:16px}

调用方法:

  1. $.kw.alert("提示内容")
  2. $.kw.alert("提示内容","系统提示")//修改弹出框提示标题
  3. $.kw.comport("提示内容");

给Jquery添加alert,prompt方法,类似系统的Alert,Prompt,可以响应键盘,支持拖动的更多相关文章

  1. jquery添加属性的方法

    $("#id" ).prop('checked', true); $("#id" ).attr('checked', 'true');

  2. jQuery自定义alert,confirm方法及样式

    学过JavaScript的都知道,alert().confirm()都是window对象特有的方法,而这两个方法我们平时使用的频率也很高,但是比较扎心的就是他自带的样式太... 因此,我整理了一个比较 ...

  3. 如何为jquery添加方法

    以下内容引自一位网友的帖子: jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery ...

  4. element弹框的的this.$alert、this.$prompt方法用法

    调用$alert方法即可打开消息提示,它模拟了系统的 alert,无法通过按下 ESC 或点击框外关闭 调用$prompt方法即可打开消息提示,它模拟了系统的 prompt

  5. 为jQuery添加Webkit的触摸方法支持

    前些日子收到邮件,之前兼职的一个项目被转给了其他人,跟进的人来问我相关代码的版权问题. 我就呵呵了. 这段代码是我在做13年一份兼职的时候无聊加上去的,为jQuery添加触摸事件的支持.因为做得有点无 ...

  6. jquery 添加节点的几种方法介绍

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  7. 原生JS添加节点方法与jQuery添加节点方法的比较及总结

    一.首先构建一个简单布局,来供下边讲解使用 1.HTML部分代码: <div id="div1">div1</div> <div id="d ...

  8. JS添加节点方法与JQuery添加节点方法的比较及总结

    原生JS添加节点方法与JQuery添加节点方法的比较及总结   一.首先构建一个简单布局,来供下边讲解使用 1.HTML部分代码: <div id="div1">div ...

  9. 通示jQuery实例方法,未DOM对象添加多个方法

    <script type="text/javascript"> /* * 通示jQuery实例方法,未DOM对象添加多个方法 * 用按钮做多个事件的调用 */ (fun ...

随机推荐

  1. AMQ学习笔记 - 01. 相关背景

    概述 介绍中间件.MOM.JMS.ActiveMQ,及相互的关系. 中间件 由于业务的不同.技术的发展.硬件和软件的选择有所差别,导致了异构组件或应用并存的局面.要使这些异构的组件协同工作,一个有效的 ...

  2. (转)一网打尽当下NoSQL类型、适用场景及使用公司

    摘要:对比传统关系型数据库,NoSQL有着更为复杂的分类——键值.面向文档.列存储以及图数据库.这里就带你一览NoSQL各种类型的适用场景及一些知名公司的方案选择. 在过去几年,关系型数据库一直是数据 ...

  3. GNU Binutils工具

    参考<程序员的自我修养---连接.装载与库> 以下内容转贴自 http://www.cnblogs.com/xuxm2007/archive/2013/02/21/2920890.html ...

  4. The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.问题解决

    didFailLoadWithError(): Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loa ...

  5. MongoDB 备份方法

    翻译自 http://docs.mongodb.org/manual/core/backups/ 有以下几种方法来备份MongoDB群集: 通过复制底层数据文件来备份 通过mongodump来备份 通 ...

  6. QA如何增强网站建设公司竞争力

    在上一篇文章<QA在网站建设公司中的作用>中我们已经详细说了QA的作用,不过有一点没有明确说明,也就是只有在超高速发展的网站建设公司中才会充分体现QA的价值.这并不是说在发展稳定的公司或低 ...

  7. 虚拟局域网VLAN

    6.5.1配置路由器广域网端口的PPP封装 (1)配置路由器A: Router>enable Router#config Router_config#hostname Router-A Rout ...

  8. Oracle 表的访问方式(2)-----索引扫描

    索引扫描(Index scan) 我们先通过index查找到数据对应的rowid值(对于非唯一索引可能返回多个rowid值),然后根据rowid直接从表中得到具体的数据,这种查找方式称为索引扫描或索引 ...

  9. URL地址下载图片到本地

    package test.dao; import eh.base.dao.DoctorDAO; import eh.entity.base.Doctor; import junit.framework ...

  10. 学习asp.net mvc5心得

    前几天时间大体学习了一下asp.net mvc5的应用,感觉最主要的就是要区分这以模式设计,其他的都是在asp.net下的基础操作 1参数的传递注意 2路由的设置规则 3model的应用