live()替代on()不工作jQuery 1.9

Déj*_*ond 5 javascript ajax jquery

我有代码做ajax方法加载新内容
但我有新内容的问题,链接没有应用我做了像
preventDefault和ajax方法,只是在加载新内容
点击链接后,使页面重新加载就像有没有ajax代码
在JQ 1.8中使用live()方法工作,但在更新jQuery之后,没有按照on()方法工作

旧代码工作正常,没有任何问题

<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>
Run Code Online (Sandbox Code Playgroud)

新的更新代码

<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>
Run Code Online (Sandbox Code Playgroud)

问题就在这里

$('.nav-menu li a,#nav-below a,#main a').on('click',function(e) {
Run Code Online (Sandbox Code Playgroud)

谢谢 :)

ale*_*lex 26

您不仅仅是s/live/on,您需要阅读文档on()以查看更新代码所需的更改(on与旧代码类似delegate()).

如果你想使它完全live(),请尝试...

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

但是,如果可以的话,最好选择最常见的持久性祖先.这意味着事件不必一直document处理.例如,body可能涵盖99.9%的情况.但是,它可能更像#some-container.