如何在函数中创建jQuery Dialog

nim*_*rod 16 jquery-ui

有谁知道如何在函数中创建jQuery对话框?我找不到设置消息的属性...在我发现的每个例子中,对话框已经静态写入div-tag中的代码中.但是,我想用它来创建它,所以我需要知道如何在函数中创建一个对话框.

设置标题没问题:

    <script>
    // increase the default animation speed to exaggerate the effect
    $.fx.speeds._default = 1000;
    $(function() {
        $( "#dialog" ).dialog({
            autoOpen: false,
            show: "blind",
            hide: "explode"
        });

        $( "#opener" ).click(function() {
            //$( "#dialog" ).dialog( "open" );
            $( this ).dialog({ title: 'Please confirm deletion!' });
            return false;
        });
    });
    </script>
</head>
<body>
Run Code Online (Sandbox Code Playgroud)

我的文档和一些例子在这里.

谢谢你帮帮忙.

干杯,doonot

============================= [解决方案] ================== ===================

感谢所有回答此问题的人.这就是我想要的方式:

    function createDialog(title, text) {
    return $("<div class='dialog' title='" + title + "'><p>" + text + "</p></div>")
    .dialog({
        resizable: false,
        height:140,
        modal: true,
        buttons: {
            "Confirm": function() {
                $( this ).dialog( "close" );
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

它可以像这样调用(点击图像):

<img src="delete.png" onClick="createDialog('Confirm deletion!', 'Do you really want to delete this package?')">
Run Code Online (Sandbox Code Playgroud)

Luk*_*uke 13

function createDialog(title, text, options) {
    return $("<div class='dialog' title='" + title + "'><p>" + text + "</p></div>")
    .dialog(options);
}
Run Code Online (Sandbox Code Playgroud)


Jar*_*ish 6

这是一个简单的例子:

function openDialog(message) {
    if ($('#dialog').length == 0) {
        $(document.body).append('<div id="dialog">'+message+'</div>');
    } else {
        $('#dialog').html(message);
    }
    $( "#dialog" ).dialog({
        autoOpen: false,
        show: "blind",
        hide: "explode"
    });
    $( "#dialog" ).dialog("open");
}
Run Code Online (Sandbox Code Playgroud)