2012-12-11 92 views
0

我試圖通過jQuery colorbox加載一個名爲#submit-text-form的表單。但是,colorbox最終會加載原始鏈接所針對的整個頁面。我按照指示here,但它仍然加載整個頁面。關於我可能做錯什麼的想法?jQuery Colorbox:通過ajax加載單個ID問題(不是整頁)

$(function(){ 
$('#submit-link').colorbox({ 
opacity: 0.3, 
transition: 'fade', 
href: $("#submit-link").attr('href') + "#submit-text-form", 
}); 
}); 
+1

在目標頁面上是否存在具有該ID的元素? –

+0

@當然是:p – Michelle

+0

有沒有在螢火蟲中顯示任何錯誤 – muthu

回答

1

試試這個:

$(function(){ 
$('#submit-link').colorbox({ 
opacity: 0.3, 
transition: 'fade', 
href: $("#submit-link").attr('href') + " #submit-text-form", 
}); 
}) 

需要有對你的選擇" #submit-text-form之前的空間。 Colorbox使用jQuery的load()方法處理ajax,所以它的工作原理是一樣的。

+0

非常感謝!這樣一個簡單的解決方案,它的工作:D – Michelle

0

當您打開應該直接從瀏覽器顯示在colorbox中的頁面時,應該只看到裏面的頁面。如果沒有,並且仍然需要加載整個頁面並顯示其中的一部分,使用ajax獲取數據,在成功獲取DOM選擇器所需的部分後,將其顯示在colorbox中。

像這樣:

$.get($("#submit-link").attr('href'), 
    function(response) { 
     //Load the AJAX response into a jQuery variable 
     var $html = $(response); 
     //Get the portion which you need to show inside the colorbox 
     $html.find('#submit-text-form').colorbox({ opacity: 0.3, transition: 'fade'); 
     //Append it to body. (I didn't use colorbox before.) 
     $('body').append($html); 
}); 

注意:我沒有測試它,我沒有顏色框前使用,但是這是你想要我的思維方式。

+0

我不遵循最後一句:/ – Michelle