Any JQuery alert() replacement for JavaScript's native one?

前端 未结 9 873
心在旅途
心在旅途 2021-02-06 16:32

I would like to replace the native javascript alert() with my own, so that I would be able to control the theme and have it more JQueryUI look and feel. I\'ve tried numerous alt

9条回答
  •  野的像风
    2021-02-06 16:51

    The native alert() brings the browser to a dead halt. You will not find any third party libraries that do that, because it's not possible.*


    Edit

    I threw together a quick demo of how you can use a single jQuery dialog instead of an alert.

    var alertManager = (function() {
        var _queue = [],
            _opts = {
                modal: true,
                autoOpen: false,
                buttons: {
                    OK: function ()
                    {
                        $(this).dialog('close');
                        var next = _queue.shift();
                        if (typeof next === 'string')
                        {
                            _dialog.text(next).dialog('open');
                        }
                    }
                }
            },
            _dialog = $('
    ').dialog(_opts), _self = {}; _self.show = function (message) { if (_dialog.dialog('isOpen')) { _queue.push(String(message)); } else { _dialog.text(message).dialog('open'); } } return _self; }()); $('#clicky').click(function () { alertManager.show('alert numero uno'); alertManager.show('alert #2'); alertManager.show({foo: 'bar'}); alertManager.show(document.getElementById('clicky')); alertManager.show('last one'); });

    Hot demo action over here →

    You could also turn this into a jQuery plugin pretty easily.


    *though you could fake it with a while loop that spins while the dialog is open. I do not recommend this.

提交回复
热议问题