2010-06-16 71 views
6

是否可以使用fancybox从另一个页面加载特定的div #id而不是通过iframe加载整个页面?jQuery fancybox - iframe中的目标特定div #id

例如,我可以用

$('#link-whole').fancybox({ 
       'width'    : '80%', 
       'height'   : '80%', 
       'type'    : 'iframe', 
      }); 

<a id="link-whole" href="somepage.html">link 1</a> 

所有“somepage.html”加载到的iframe,但我如何从一个div id为加载JUST内容“目标”(例如)?

回答

14

如果页面与打开fancybox的页面位于相同的域,那么您应该能够使用jQuery.ajax的dataFilter选项将返回的数据过滤到所需的目标ID。

$('#link-whole').fancybox({ 
    'width': '80%', 
    'height': '80%', 
    'type': 'ajax', 
    'ajax': { 
     dataFilter: function(data) { 
      return $(data).find('#yourTargetID')[0]; 
     } 
    } 
}); 
+0

完美的目标页面的特定部分!谢谢!! 如果目标页面在另一个域上,有没有办法实现这一点? – 2010-06-16 18:23:07

+0

有了JSONP请求和使用http://pipes.yahoo.com/pipes/你可以做到。 – PetersenDidIt 2010-06-16 18:36:51

4

如果找方法不为你工作,尝试在dataFilter对象内部线filter

return $(data).filter('#yourTargetID')[0]; 
0

你可以使用beforeShow

    function opendialog(mypage) { 
        var linktoopen=mypage ; 
        $.fancybox({       
        'autoScale': false, 
        'transitionIn': 'elastic', 
        'transitionOut': 'elastic', 
        'speedIn': 500, 
        'speedOut': 300,      
        'width'   : 800, 
        'height'  : 410, 
        'autoDimensions': false, 
        'centerOnScroll': true, 
        'scrolling' : 'no', 
        'type' : 'iframe', 
        'href' : linktoopen, 
         beforeShow: function(){ 
          $(".fancybox-iframe").css({ 
          margin : "-380px 0 0", 
         height:"790px" 
         }); 
        } 
        }); 
        }