2013-05-09 13 views
5

を働いていない新しいコンテンツ
をロードするためのAJAXの方法を実行します。しかし、私は新しいコンテンツに問題があり、リンクは
でpreventDefaultとAjaxのように私がしたアクションを適用しません新しいコンテンツをロードした直後にメソッドが呼び出されました
リンクをクリックするとページがリロードされるようになりました。
JQ 1.8ではlive()メソッドを使用して動作しますが、jQueryを更新した後は()メソッド
)(ライブ選択肢は、私は、コードを持っているjQueryの1.9

古いコードは正常に動作しており、問題はありません

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> 
<script> 
function loadContent(url){ 
     $.ajax({ 
      url: url, 
      dataType: 'html', 
      cache: false 
     }).done(function(html){ 
      var $html = $(html); 

      $('title').html($html.filter('title').text()); 
      $('#main').replaceWith($html.find('#main')); 
      $('.nav-menu').replaceWith($html.find('.nav-menu')); 
      $('.nav-below').replaceWith($html.find('.nav-below')); 
     }); 
} 

$(function(){ 

    // Get The Content Action 
    $('.nav-menu li a,#nav-below a,#main a').live('click',function(e) { 
     href = $(this).attr("href"); 

     loadContent(href); 

     history.pushState('', 'New URL: '+href, href); 
     e.preventDefault(); 

     // go top after showing the content 
     $('html, body').animate({scrollTop:0}, 'slow'); 
    }); 

    // THIS EVENT MAKES SURE THAT THE BACK/FORWARD BUTTONS WORK AS WELL 
    window.onpopstate = function(event){ 
     loadContent(location.pathname); 
     $('html, body').animate({scrollTop:0}, 'slow'); 
    }; 

}); 
</script> 

右ここに新しい更新されたコード

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<script> 
function loadContent(url){ 
     $.ajax({ 
      url: url, 
      dataType: 'html', 
      cache: false 
     }).done(function(html){ 
      var $html = $(html); 

      $('title').html($html.filter('title').text()); 
      $('#main').replaceWith($html.find('#main')); 
      $('.nav-menu').replaceWith($html.find('.nav-menu')); 
      $('.nav-below').replaceWith($html.find('.nav-below')); 
     }); 
} 

$(function(){ 

    // Get The Content Action, ‡‡=‡=‡=‡=L0000K HERE=‡=‡=‡=‡‡ Not workin JQ 1.9 
    $('.nav-menu li a,#nav-below a,#main a').on('click',function(e) { 
     href = $(this).attr("href"); 

     loadContent(href); 

     history.pushState('', 'New URL: '+href, href); 
     e.preventDefault(); 

     // go top after showing the content 
     $('html, body').animate({scrollTop:0}, 'slow'); 
    }); 

    // THIS EVENT MAKES SURE THAT THE BACK/FORWARD BUTTONS WORK AS WELL 
    window.onpopstate = function(event){ 
     loadContent(location.pathname); 
     $('html, body').animate({scrollTop:0}, 'slow'); 
    }; 

}); 
</script> 

問題

$('.nav-menu li a,#nav-below a,#main a').on('click',function(e) { 

ありがとうございました:)

+0

_ "... ...うまくいかない..." _。構文が間違っていると、委任で 'on'のマニュアルをチェックしてください。 – elclanrs

+0

それはイベントの委任が 'on'でどのように行われるかではありません。 http://api.jquery.com/live/を読んだことがありますか? – Bergi

+0

Btw、何百もの類似の質問と重複があります:http://stackoverflow.com/questions/8867194/jquery-on-not-working-with-dynamic-dom-html、http://stackoverflow.com/questions/ 9484295/jquery-click-not-working-for-dynamically-created-itemsについては、人々が5分間検索しただけでは、「jquery stackoverflowを作業していない」と2つの非常に最初の結果を探して見つけました。 – elclanrs

答えて

24

をあなたは単にs/live/onをしないで、あなたは、ドキュメントを読む必要がありますon()を使用して、コードを更新するために必要な変更を確認してください(onは古いdelegate()に似ています)。

あなたはそれが正確live()などを仕事にしたい場合は、してみてください...

$(document).on('click', '.nav-menu li a,#nav-below a,#main a', function(e) { }); 

しかし、あなたができるならば、あなたが最も一般的な持続性の祖先を選択する方がいいでしょう。これは、イベントが処理されるまでには最大でdocumentになる必要はないことを意味します。たとえば、bodyはおそらく99.9%の状況をカバーします。しかし、それはおそらく#some-containerのようです。好き

$.fn.follow = function (event, callback) { 
    $(document).on(event, $(this).selector, callback); 
} 

とあなたがそれを使用することができます:あなたはこのようなものを使用することができます簡単に使用のための

+0

ありがとう、私は正しく理解していない、あなたは私にうまく説明した –

0

あなたはまだあなたのプラグインでイベントデータを使用することができます

$("#myselector").follow("click",function(){ 
    /*some callback code*/ 
}); 

$("#expform").follow("submit",function(e){ 
     e.preventDefault(); 
}); 
関連する問題