tôi có mã làm phương pháp ajax cho tải nội dung mới
Nhưng tôi có vấn đề với các nội dung mới, các liên kết không áp dụng các hành động tôi đã làm như
preventDefault và ajax phương pháp, ngay sau khi tải nội dung mới
nhấp vào liên kết làm cho trang tải lại như không có mã ajax ở tất cả
Trong JQ 1.8 làm việc grate với phương thức live(), nhưng sau khi cập nhật jQuery, không hoạt động() phương thức
live() thay thế vào() không làm việc jQuery 1.9
Mã cũ hoạt động đúng và không có vấn đề với nó
<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>
Các cập nhật mới đang
<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>
Vấn đề ngay tại đây
$('.nav-menu li a,#nav-below a,#main a').on('click',function(e) {
Cảm ơn bạn :)
_ "... không làm việc như nó nên ..." _. Cú pháp sai, kiểm tra hướng dẫn sử dụng cho 'on' với ủy quyền. – elclanrs
Đó không phải là cách ủy nhiệm sự kiện được thực hiện với 'on'. Bạn đã đọc http://api.jquery.com/live/ chưa? – Bergi
Btw, có hàng trăm câu hỏi và trùng lặp tương tự: http://stackoverflow.com/questions/8867194/jquery-on-not-working-with-dynamic-dom-html, http://stackoverflow.com/questions/ 9484295/jquery-click-not-working-cho-động-tạo-mục. Nếu chỉ những người tìm kiếm trong 5 phút ... Tôi tìm thấy nó bằng cách tìm kiếm "trên không làm việc jquery stackoverflow" và baam hai kết quả đầu tiên. – elclanrs