简体   繁体   English

用jquery ui显示没有元素的模态对话框?

[英]Showing a modal dialog with jquery ui without an element?

I'm making a small jquery application. 我正在制作一个小的jquery应用程序。 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? 只是调用一个对话框并传递标题,文本和选项等参数?

When you create a jQuery UI dialog box, current versions (1.8.*) automatically add the dialog to the body. 当您创建jQuery UI对话框时,当前版本(1.8。*)会自动将对话框添加到正文。

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! 您应该确保在关闭对话框的情况下调用.remove()以删除新元素!

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. 有关完整的工作演示,请参见http://jsfiddle.net/alnitak/G3GRZ/

去做就对了:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM