【问题标题】:Fancybox - How to change the content of iframe?Fancybox - 如何更改 iframe 的内容?
【发布时间】:2012-11-10 13:02:28
【问题描述】:

我需要在 iframe fancybox 窗口中设置或更新自定义 HTML 代码。 那我该怎么做?! “内容”选项不起作用! 在本地主机上测试。

  $("test").fancybox({
  'width'    : 300,
  'height'   : 300,
        'autoScale'      : false,
        'type' : 'iframe',
        'content' : '<H5> It`s does not work</h5>'
 });

如果无法更改跨域 iframe 的内容(如我的情况),那么,请告诉我如何在同一个域上进行。 例如,我们在 http://localhost/page1.html 和 iframe 链接到 http://localhost/page2.html

【问题讨论】:

    标签: javascript jquery iframe fancybox frame


    【解决方案1】:

    当fancybox-iframe 已经打开时,您可以使用$('.fancybox-iframe') 访问iframe-element。

    如果 iframe 内的文档位于同一个域中,您可以使用 jQuery 更改内容:

    $('body',$('.fancybox-iframe').contents()).html('new content');
    

    当fancybox-iframe还没有打开时,你可以在afterLoad-callback里面做同样的事情:

    $("test").fancybox({
      'width'    : 300,
      'height'   : 300,
            'autoScale'      : false,
            'type' : 'iframe',
            'href':'page2.html',
            afterLoad:function(){$('body',$('.fancybox-iframe').contents())
                                  .html('new content');}
     });
    

    是的:跨域 iframe 的内容无法更改(在这种情况下,您只能将全新的文档加载到 iframe 中)。

    【讨论】:

    • 感谢您的回复!奇怪的是,但第一个变体(我需要的)不起作用......我只需要添加一些包含文本的
      标记。花了 9 个小时,什么都没有......
    • 我尝试制作 append($elem).css().html() 但它也不起作用。我使用了其他元素 - fancybox-content。是的,元素只有在你出现时才会出现设置 append(), - not css() 和其他。
    • @Dr.Molle 我试过 $('body',$('.fancybox-iframe').contents()) .html('new content');在 ajax 调用返回新的 html 之后,它在 iframe 中没有改变。我还需要别的吗?
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签