0

我有这个功能:

// AJAX MESSAGES SHOWING
show_msg.on('click', function(e){
    var $this = $(this),
    url = $this.attr('href'),
    url_info =  url + ' .basic_info > *',
    url_msg =  url + ' .contact_messages > *';

    basic_info.slideUp('fast', function(){
            basic_info.empty().load(url_info, function(){
                basic_info.slideDown('fast');
        });
    });

    contact_messages.slideUp('fast', function(){
            contact_messages.empty().load(url_msg, function(){
                contact_messages.slideDown('fast');
        });
    });
    return false;
});

它正在工作,但我使用了两次负载,我想这不是很有效的方法。有什么方法可以加载一次数据,然后从该数据中选择您需要的内容吗?

4

1 回答 1

1

你可以这样做:

(function(){

    var basicInfoLoaded = false, contactMessageLoaded = false;

// AJAX MESSAGES SHOWING
show_msg.on('click', function(e){
    var $this = $(this),
    url = $this.attr('href'),
    url_info =  url + ' .basic_info > *',
    url_msg =  url + ' .contact_messages > *';

    basic_info.slideUp('fast', function(){
                    if(basicInfoLoaded)
                        basic_info.slideDown('fast');
                    else{
                        basicInfoLoaded = true;
            basic_info.empty().load(url_info, function(){
                    basic_info.slideDown('fast');
            });
                    }
    });

    contact_messages.slideUp('fast', function(){
                    if(contactMessageLoaded)
                        contact_messages.slideDown('fast');
                    else{
                            contactMessageLoaded = true;
            contact_messages.empty().load(url_msg, function(){
                contact_messages.slideDown('fast');
                });
                    }
    });
    return false;
});

})();
于 2013-04-25T12:27:38.183 回答