jQuery UI Dialog使用

自定义一个扩展js程序:

jQuery.extend(jQuery, {
  // jQuery UI alert弹出提示
  jqalert: function(text, title, fn) {
    var html =
    '<div class="dialog" id="dialog-message">' +
    '  <p>' +
    '    <span class="ui-icon ui-icon-circle-check" style="float: left; margin: 0 7px 0 0;"></span>' + text +
    '  </p>' +
    '</div>';
    return $(html).dialog({
      //autoOpen: false,
      resizable: false,
      modal: true,
      show: {
        effect: 'fade',
        duration: 300
      },
      title: title || "提示信息",
      buttons: {
        "确定": function() {
          var dlg = $(this).dialog("close");
          fn && fn.call(dlg);
        }
      }     
    });
  },
  // jQuery UI alert弹出提示,一定间隔之后自动关闭
  jqtimeralert: function(text, title, fn, timerMax) {
    var dd = $(
    '<div class="dialog" id="dialog-message">' +
    '  <p>' +
    '    <span class="ui-icon ui-icon-circle-check" style="float: left; margin: 0 7px 0 0;"></span>' + text +
    '  </p>' +
    '</div>');
    dd[0].timerMax = timerMax || 3;
    return dd.dialog({
      //autoOpen: false,
      resizable: false,
      modal: true,
      show: {
        effect: 'fade',
        duration: 300
      },
      open: function(e, ui) {
        var me = this,
          dlg = $(this),
          btn = dlg.parent().find(".ui-button-text").text("确定(" + me.timerMax + ")");
        --me.timerMax;
        me.timer = window.setInterval(function() {
          btn.text("确定(" + me.timerMax + ")");
          if (me.timerMax-- <= 0) {
            dlg.dialog("close");
            fn && fn.call(dlg);
            window.clearInterval(me.timer); // 时间到了清除计时器
          }
        }, 1000);
      },
      title: title || "提示信息",
      buttons: {
        "确定": function() {
          var dlg = $(this).dialog("close");
          fn && fn.call(dlg);
          window.clearInterval(this.timer); // 清除计时器
        }
      },
      close: function() {
        window.clearInterval(this.timer); // 清除计时器
      }
    });
  },
  // jQuery UI confirm弹出确认提示
  jqconfirm: function(text, title, fn1, fn2) {
    var html =
    '<div class="dialog" id="dialog-confirm">' +
    '  <p>' +
    '    <span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>' + text +
    '  </p>' +
    '</div>';
    return $(html).dialog({
      //autoOpen: false,
      resizable: false,
      modal: true,
      show: {
        effect: 'fade',
        duration: 300
      },
      title: title || "提示信息",
      buttons: {
        "确定": function() {
          var dlg = $(this).dialog("close");
          fn1 && fn1.call(dlg, true);
        },
        "取消": function() {
          var dlg = $(this).dialog("close");
          fn2 && fn2(dlg, false);
        }
      }
    });
  },
  // jQuery UI 弹出iframe窗口
  jqopen: function(url, options) {
    var html =
    '<div class="dialog" id="dialog-window" title="提示信息">' +
    ' <iframe src="' + url + '" frameBorder="0" style="border: 0; " scrolling="auto" width="100%" height="100%"></iframe>' +
    '</div>';
    return $(html).dialog($.extend({
      modal: true,
      closeOnEscape: false,
      draggable: false,
      resizable: false,
      close: function(event, ui) {
        $(this).dialog("destroy"); // 关闭时销毁
      }
    }, options));
  },
  // jQuery UI confirm提示
  confirm: function(evt, text, title) {
    evt = $.event.fix(evt);
    var me = evt.target;
    if (me.confirmResult) {
      me.confirmResult = false;
      return true;
    }
    jQuery.jqconfirm(text, title, function(e) {
      me.confirmResult = true;
      if (e) {
        if (me.href && $.trim(me.href).indexOf("javascript:") == 0) {
          $.globalEval(me.href);
          me.confirmResult = false;
          return;
        }
        var t = me.type && me.type.toLowerCase();
        if (t && me.name && (t == "image" || t == "submit" || t == "button")) {
          __doPostBack(me.name, "");
          me.confirmResult = false;
          return;
        }
        if (me.click) me.click(evt);
      }
      return false;
    });
    return false;
  }
});

调用方法如下:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title>Fancy Validate - jQuery UI Dialog</title>
  <link href="css/cupertino/jquery-ui-1.8.18.custom.css" rel="stylesheet" />
  <script src="js/jquery-1.7.1.min.js"></script>
  <script src="js/jquery-ui-1.8.18.custom.min.js"></script>
  <script src="demo.js"></script>
  <style>
    body { font-size: 12px; }
     /*jQuery UI fakes*/
    .ui-widget { font-size: 1em; }
    .ui-dialog .ui-dialog-buttonpane { padding-top: .1em; padding-bottom: .1em; }
  </style>
  <script>
    $(function() {
      $("#button1").click(function() {
        $.jqalert("这是普通提示!");
      });
      $("#button2").click(function() {
        $.jqtimeralert("这是自动关闭的提示!", "自动关闭提示",
          function() {
            $.jqalert("时间到");
          });
      });
      $("#button3").click(function() {
        $.jqconfirm("确定要这么做吗?", "确认提示",
          function() {
            $.jqalert("点了确定");
          },
          function() {
            $.jqalert("点了取消");
          });
      });
      $("#button4").click(function(e) {
        if ($.confirm(e, "确定要这么做吗?"))
          $.jqalert("点了确定");
      });
      $("#button5").click(function(e) {
        $.jqopen("http://www.cnblogs.com/yxlblogs", { title: "我的博客", 700, height: 500 });
      });
    });
  </script>
</head>
<body>
  <form action="?" id="fancyform">
  <fieldset>
    <legend>jQuery UI Dialog</legend>
    <div>
      <input type="button" id="button1" value="普通提示" />
      <input type="button" id="button2" value="自动关闭提示" />
      <input type="button" id="button3" value="确认提示" />
      <input type="button" id="button4" value="确认提示2" />
      <input type="button" id="button5" value="打开窗口" />
    </div>
  </fieldset>
  </form>
</body>
</html>

原文地址:https://www.cnblogs.com/yxlblogs/p/3038282.html