2011-12-14 81 views
1

這裏有一個教程:http://perishablepress.com/slide-fade-content/從隱藏的div加載內容?

,它提供的代碼是:

$(document).ready(function(){ 
    // Ajax Slide & Fade Content with jQuery @ http://perishablepress.com/slide-fade-content/ 
    $('.more').live('click',function(){ 
     var href = $(this).attr('href'); 
     if ($('#ajax').is(':visible')) { 
      $('#ajax').css('display','block').animate({height:'1px'}).empty(); 
     } 
     $('#ajax').css('display','block').animate({height:'200px'},function(){ 
      $('#ajax').html('<img class="loader" src="http://example.com/slide-fade-content/loader.gif" alt="">'); 
      $('#ajax').load('http://example.com/slide-fade-content/slide-fade-content.html.html #'+href,function(){ 
       $('#ajax').hide().fadeIn().highlightFade({color:'rgb(253,253,175)'}); 
      }); 
     }); 
     return true; 
    }); 
}); 

這將從外部文件加載的內容,有沒有辦法做同樣的事情,但加載從內容一個隱藏的div在同一頁上?

+0

檢查出JQuery的LIB您正在使用的「阿賈克斯」部分;) – 2011-12-14 18:11:16

回答

4

更換

$('#ajax').load('http://example.com/slide-fade-content/slide-fade-content.html.html #'+href,function(){ 

var contentTobeLoaded=$("#myHiddenDivId").html() 
$('#ajax').html(contentTobeLoaded).fadeIn(600,function(){ 

假設你有隱藏的div id爲myHiddenDivId

編輯:由於從提供您的意見和樣本的鏈接,這是我的更新解決方案

1)將每個項目的內容放在單獨的div中並隱藏它。這個div應該有唯一的ID 2)當你點擊鏈接時,你會得到id並從隱藏的div中加載相應的內容。

HTML

<div id="divHiddenContainer" style="display:none;"> 
     <div id="divItem1"><span style="background-color:Gray;">God, My description about item 1 goes here</span></div> 
     <div id="divItem2"><span style="background-color:yellow;">Brother,My description about item 222 goes here</span></div> 
     <div id="divItem3"><span style="background-color:orange;">Hello,My description about item 333 goes here</span></div> 
    </div> 

    <a href="#" class="aItemLnk" id="a1">Item 1</a> 
    <a href="#" class="aItemLnk" id="a2">Item 1</a> 
    <a href="#" class="aItemLnk" id="a3">Item 1</a> 

    <h3>Content goes here</h3> 
    <div id="ajax"></div> 

的Javascript

$(".aItemLnk").click(function() { 

    var id = $(this).attr("id").replace(/^.(\s+)?/, ""); 
    var contentTobeLoaded = $("#divItem" + id).html();   

    $('#ajax').html(contentTobeLoaded).fadeIn(600, function() { 
     //do whatever you want after fadeIn 
    }); 
}); 

這裏是工作樣例:http://jsfiddle.net/9xZrq/

第二個樣本,其淡入之前有淡出:http://jsfiddle.net/9xZrq/1/

你可以改變你需要的淡入從600到1200或1500的延遲或任何你想要的

請注意,您需要有鏈接ID和隱藏的DIV ID之間有着某種聯繫,這樣就可以找出哪些DIV被展示。

0

我假設你div已經包含他的數據,你只是想表明它,所以你可以使用:

$('#id_of_your_div').show().fadeIn(); 

或者我已經錯了你,你想從div內容加載到另一個?所以你可以用html()檢索他的內容。

0

如果我理解正確的...只是打電話給你的對象html屬性做到這一點...

$('#yourdiv').html(); 

這將返回div無論是隱藏或內容沒有。

-1

你應該能夠相當簡單地加載隱藏div的內容,因爲你使用jquery來獲得$()方法。

給div一個id,然後用$('#id-of-element').innerHTML會給你隱藏div的內容。

+0

的innerHTML不再是首選的HTTP:/ /www.javascriptkata.com/2007/04/17/do-not-use-the-innerhtml-property-on-html-objects/ – 2011-12-14 18:17:11

+0

$('#id-of-element')。html()會給你內部html – Shyju 2011-12-14 18:17:17