原始,接受的答案
什么都没有发生,因为您在 HTML id 标记中拼写错误btnClose
(您正在调用它btnCloset
)。尽管如此,它不适用于提供的代码,因为它是这样做的:当您单击btnClose
链接时,您正在创建一个 simpleModal 框#content
,并告诉它当它关闭时,它应该执行onClose
选项(这是正确的)。所以你实际上并没有告诉它在任何地方关闭,只是告诉它这是一个模式对话框。
相反,您应该像现在一样从元素中创建模式,但要与的 click 事件#content
分开进行。#btnClose
然后你应该绑定点击事件来关闭对话框。
这是一些代码
$(function() {
/* Make #content a modal */
$("#content").modal(
{
onClose: function(dialog) {
dialog.data.fadeOut('slow', function () {
dialog.container.slideUp('slow', function () {
dialog.overlay.fadeOut('slow', function () {
$.modal.close(); // must call this!
});
});
});
}
}
);
/* When #btnClose is clicked, close the modal */
$('#btnClose').click(function(e) {
$.modal.close();
});
});
附带说明,如果您将类添加
simplemodal-close
到
#btnClose
,simpleModal 将自动使其关闭对话框,您不必自己绑定点击事件。
基于反馈的新答案
好的,我误解了这个插件的工作原理,我认为它就像普通的 jQuery 对话框插件,但据我所知,目标是使现有的可见元素成为对话框,并在关闭它时将其转换回原始形式. 新代码跟踪对话框的状态(通过存储doOpen
在data
链接中并在每次单击时切换它)并打开和关闭对话框。希望这更接近您希望它的工作方式:)
$(function() {
$("#btnClose")
.data("doOpen", true)
.click( function() {
/* check whether or not we are to open or close the dialog */
var doOpen = $(this).data("doOpen");
/* toggle the data */
$(this).data("doOpen", !doOpen);
if (doOpen) {
$("#content").modal({
onClose: function(dialog) {
dialog.data.fadeOut('slow', function () {
dialog.container.slideUp('slow', function () {
dialog.overlay.fadeOut('slow', function () {
$.modal.close(); // must call this!
});
});
});
}
});
}
else {
$.modal.close();
}
});
});