2013-12-23 39 views
0

基本上,我點擊一個鏈接 - >將它的內容加載到新內容div中 - >移動並移除舊內容 - >將新內容作爲當前內容。如何在jQuery加載後重新檢查dom?

我正在使用jquery加載函數。當然,新的內容並不是由它被加載的方式看到的。我曾希望使用#page a將是最好的,因爲#page是一個恆定的dom。

這很明顯,第一次加載頁面,但從未在此之後。

我需要jQuery來識別點擊新加載的內容。我知道這是可能的,但我不能找到方法。

$('#page').append('<div id="new-content"></div>'); 
    $('#page a').on('click', function(e){ 
     console.log('click'); 
     var topHeight = $('.site-header').height(); 
     var comp = new RegExp(location.host); 
     if(comp.test($(this).attr('href'))){ 
       // a link that contains the current host   
       e.preventDefault(); 
       var contentURL = $(this).attr('href'); 
       $('#new-content').load(contentURL+" #content",function(){ 
        var newContent = $(this); 
        $('#content').first().animate({'left':'-100%'},function(){ 
         $(this).remove(); 
         newContent.find('#content').unwrap(); 
         $('#page').append('<div id="new-content"></div>'); 
         pageHeight = $('#content').height(); 
         $('#slide-out').css({'height':pageHeight}); 
        }); 
       }).css({'top':topHeight}); 
     } 
    }); 

回答

1

你必須使用的on()委派的版本,像這樣:

$('#page').on('click', 'a', function(e){ ...