最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Showing a modal dialog with jquery ui without an element? - Stack Overflow

programmeradmin5浏览0评论

I'm making a small jquery application. I need some confirmation boxes to appear. However, I don't want to have to append an element to the body just so I can open up a dialog box. Is there a way to avoid this? To just call a dialog and pass arguments such as the title and text and options?

I'm making a small jquery application. I need some confirmation boxes to appear. However, I don't want to have to append an element to the body just so I can open up a dialog box. Is there a way to avoid this? To just call a dialog and pass arguments such as the title and text and options?

Share Improve this question asked May 8, 2011 at 20:15 MatthewMatthew 15.7k28 gold badges91 silver badges124 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 15

When you create a jQuery UI dialog box, current versions (1.8.*) automatically add the dialog to the body.

So if you do:

$('<div>').dialog({modal: true})

it just works. You should ensure that you call.remove() with the dialog is closed to remove the new element, though!

function myalert(title, text) {
    var div = $('<div>').html(text).dialog({
        title: title,
        modal: true,
        close: function() {
            $(this).dialog('destroy').remove();
        },
        buttons: [{
            text: "Ok",
            click: function() {
                $(this).dialog("close");
            }}]
    })
};

myalert("Test", "This is a test modal dialog");

See http://jsfiddle.net/alnitak/G3GRZ/ for full working demo.

Just do it:

$('<div>My dialog text.</div>').dialog({ modal: true });
发布评论

评论列表(0)

  1. 暂无评论