jquery - Fancybox v2 关闭对话框,内容中带有 html 按钮

标签 jquery fancybox fancybox-2

我正在尝试关闭内容中带有 html 按钮的 fancybox 对话框,而不使用助手或 tpl。但没有成功。

var modal = function () { 
   $.fancybox({
      'minHeight'  : 100,
      'autoScale'  : true,
      'autoSize'   : true,
      'autoHeight' : true,
      'autoWidth'  : true,
      'fitToView'  : true,
      'closeBtn'   : false,
      'modal'      : true,
       beforeShow: function()
       {
            $('#modal').html(msg);
       }
    });
}

HTML

<div id="open">OPEN</div>
<div id="modal"></div>

我的代码

$('#open').on('click', function ()
{
    var btn = '<br><a href="javascript:;">Close</a>';
    modal("here the html content" + btn);
});

你能怎么办?谢谢

最佳答案

我会对您的代码进行一些调整。

  1. 既然您调用 modal像这样

    modal("here the html content" + btn);
    

    ...您需要在函数中传递参数,例如

    var modal = function(msg) { ... }
    
  2. 无论您填写#modal类似容器

    $('#modal').html(msg);
    

    ...您仍然需要通过添加href来告诉fancybox目标该容器作为fancybox内容。 API 选项如:

    href: "#modal"
    

    顺便说一句,使用beforeLoad回调而不是 beforeShow.html() 填充内容方法如

    beforeLoad: function () {
        $('#modal').html(msg);
    }
    
  3. 您的btn缺少目标 $.fancybox.close()对象方法click所以将其更改为:

    var btn = '<br><a href="javascript:jQuery.fancybox.close();">Close</a>';
    

所以你的最终代码应该如下所示:

var modal = function(msg) {
    $.fancybox({
        minHeight: 100,
        // autoScale: true, // not a valid option for v2.x
        autoSize: true,
        // autoHeight: true, // not needed since autoSize is true
        // autoWidth: true,
        fitToView: true,
        // closeBtn: false, // not needed since modal is true
        modal: true,
        href: "#modal",
        beforeLoad: function () {
            $('#modal').html(msg);
        }
    });
}
jQuery(document).ready(function ($) {
    $('#open').on('click', function () {
        var btn = '<br><a href="javascript:jQuery.fancybox.close();">Close</a>';
        modal("here the html content" + btn);
    });
}); // ready

参见JSFIDDLE

注意对某些 API 选项的评论

关于jquery - Fancybox v2 关闭对话框,内容中带有 html 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26591626/

相关文章:

javascript - 加载插件时出错

javascript - FancyBox - 单击按钮时重新加载父页面

带有 Fancybox 的 HTML5 视频

javascript - Fancybox V2 fit content width iframe

javascript - fancybox 确保 fancybox-image 不被缓存

jquery - 如何让弹窗每隔一定时间只出现一次

JQuery:FullCalendar 插件:事件不显示在周 View 和日 View 中,但显示在月 View 中

javascript - 使用 localstorage HTML5 保存表单

javascript - JQuery FancyBox、Prepend 函数和 <a href> 标签在 IE 7 和 8 中不起作用..(其他浏览器功能正常)

javascript - FancyBox - "Don' t 再次显示此消息“按钮?