1

我有这个代码

$("body").on({
    click: function(event){
        event.preventDefault();
        var aLink = $(this).attr("href");
        $("#content").load(aLink+" #loader", function(){
          //Callback here
        });
        $("#crumbbar").load(aLink+' .breadcrumbs', function(){
          //Callback here
        });
    }
}, "a");

可以以我只有 1 个加载命令的方式进行优化吗?

4

1 回答 1

2

To only do the ajax call once, you would have to use another of jQuery's ajax functions, like $.get, which load() is a shortcut for:

$(document).on('click', 'a', function(e) {
    e.preventDefault();
    var aLink = $(this).attr("href");
    $.get(aLink, function(data) {
        $("#content").html($(data).find('#loader'));
        $("#crumbbar").html($(data).find('.breadcrumbs').first())
    });
});
于 2012-09-09T15:28:04.360 回答