这几天在做一个交互的原型,第一次大量采用jQueryUI,确实非常方便好用,其中一些功能点用到了弹出确认提示框,看了jQueryUI Dialog的例子,效果还不错,就是用起来有点儿别扭,写出的代码有点拧巴,需要再封装一下!于是就有了下面这个简单的DialogHelper辅助类,因为这篇文章分享的重点是思路,所以目前版本的代码也还非常粗糙。思路对了,后续再封装成什么样都不过是形式而已,希望这个思路能给大家点启迪,同时欢迎大家开拓思维,提出更好的改进意见。
DialogHelper的源代码如下:
04 |
DialogHelper = function () { |
09 |
this .dlgDiv = $( "<div><p><span class=\"ui-icon ui-icon-alert\" style=\"float: left; margin: 0 7px 20px 0;\"></span></p></div>" ); |
12 |
this .set_Title = function (val) { |
15 |
this .get_Title = function () { |
18 |
this .set_Msg = function (val) { |
21 |
this .get_Msg = function () { |
24 |
this .set_Buttons = function (val) { |
27 |
this .get_Buttons = function () { |
31 |
this .open = function () { |
32 |
$dlg = this .dlgDiv.clone(); |
33 |
$dlg.children().filter( "p" ).html( this .dlgDiv.children().filter( "p" ).html() + this .get_Msg()); |
43 |
title: this .get_Title(), |
44 |
buttons: this .get_Buttons() |
使用DialogHelper辅助类的代码如下:
01 |
$(document).ready( function () { |
02 |
$( '#opener' ).click( function () { |
04 |
dlgHelper = new DialogHelper(); |
07 |
dlgHelper.set_Title( "确认要删除现有项目吗?" ); |
08 |
dlgHelper.set_Msg( "执行这个操作,原来的项目将被删除,你确认要这么做吗?" ); |
09 |
dlgHelper.set_Buttons({ |
12 |
$( this ).dialog( 'close' ); |
16 |
$( this ).dialog( 'close' ); |
下载源代码: