を働いていない新しいコンテンツ
をロードするための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) {
ありがとうございました:)
_ "... ...うまくいかない..." _。構文が間違っていると、委任で 'on'のマニュアルをチェックしてください。 – elclanrs
それはイベントの委任が 'on'でどのように行われるかではありません。 http://api.jquery.com/live/を読んだことがありますか? – Bergi
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