对话框插件可以用动画的效果弹出多种类型的对话框,实现JavaScript代码中alert()
和confirm()
函数的功能,它的调用格式为:
$(selector).dialog({options});
selector参数为显示弹出对话框的元素,通常为<div>,options参数为方法的配置对象,在对象中可以设置对话框类型、“确定”、“取消”按钮执行的代码等。
例如,当点击“提交”按钮时,如果文本框中的内容为空,则通过dialog插件弹出提示框,提示输入内容不能为空,如下图所示:
在浏览器中显示的效果:
从图中可以看出,当文本框的内容为空时,调用自定义的sys_Alert函数,在该函数中再调用dialog插件的dialog()
方法,弹出带模式的显示信息对话框。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>对话框插件</title> <link href="http://www.imooc.com/data/jquery-ui.css" rel="stylesheet" type="text/css" /> <link href="style.css" rel="stylesheet" type="text/css" /> <script src="http://www.imooc.com/data/jquery-1.8.2.min.js" type="text/javascript"></script> <script src="http://www.imooc.com/data/jquery-ui-1.9.2.min.js" type="text/javascript"></script> </head> <body> <div id="divtest"> <div class="content"> <span id="spnName" class="fl">张三</span> <input id="btnDelete" type="button" value="删除" class="fr"/> </div> <div id='dialog-modal'></div> </div> <script type="text/javascript"> $(function () { $("#btnDelete").bind("click", function () { //询问按钮事件 if ($("#spnName").html() != null) { //如果对象不为空 sys_Confirm("您真的要删除该条记录吗?"); return false; } }); }); function sys_Confirm(content) { //弹出询问信息窗口 ?({ height: 140, modal: true, title: '系统提示', hide: 'slide', buttons: { '确定': function () { $("#spnName").remove(); $(this).dialog("close"); }, '取消': function () { $(this).dialog("close"); } }, open: function (event, ui) { $(this).html(""); $(this).append("<p>" + content + "</p>"); } }); } </script> </body> </html>
body { font-size: 62.5%; } #divtest { width: 282px; } #divtest .content { background-color: #eee; padding: 8px; height: 23px; line-height: 23px; } .fl { float: left; } .fr { float: right; }