我编写了一个动态创建看起来像这样的Jquery UI对话框的函数。
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* dialogConstructor()
* ++++++++++++++++++
*
* Description:
*
* This function will create a dialog based on the parameters you feed it
* This is nice because it keeps you from having to create a new dialog for every situation
* An example id construct would be "upload-excel" - which just allows this function to make unique id's.
*
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
function dialogConstructor(name,idConstruct,paragraph) {
// First step: grab the HTML inside the generic dialog container
var genericDialog = $("#dialog-generic-container").html();
// Make the id of the dialog unique
var newDialog = genericDialog.replace('id="dialog-generic"','id="dialog-' + idConstruct + '"');
// Also store that value
var dialogId = 'dialog-' + idConstruct;
// Change the Title
newDialog = newDialog.replace('title="Basic dialog"','title="' + name + '"');
// Change the id of the paragraph
newDialog = newDialog.replace('id="dialog-p"','id="dialog-p-' + idConstruct + '"');
// Change the content of the paragraph with the incoming info
newDialog = newDialog.replace('Default Paragraph',paragraph);
// Append to the html
$("#outermost-id").append(newDialog);
// Construct
$("#" + dialogId).dialog({
// Settings for the dialog
autoOpen: true,
buttons: {
Ok: function() {
$(this).dialog("close");
}
}
});
}
该函数的工作方式是在对话框中附加一个新ID,一个新标题和一个新标题,然后我附加到外部HTML本身。 我需要的唯一HTML才能使其发挥作用。
<div id="dialog-generic-container" style="display:none"> <!-- Generic Dialog Container -->
<div id="dialog-generic" title="Basic dialog">
<div class="dialog-p-container">
<p id="dialog-p">Default Paragraph</p>
</div>
</div>
</div> <!-- Close Generic Dialog Container -->
适用于谷歌浏览器和Firefox,但不 IE(在IE 10上测试)。