2010-06-16 55 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" 
         }); 
        } 
        }); 
        }