首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将ajax请求缩小到相同的调用模式

将ajax请求缩小到相同的调用模式
EN

Stack Overflow用户
提问于 2015-01-10 14:30:32
回答 1查看 577关注 0票数 0

你好,伙计们,因为内存和时间问题,我只想缩小这段代码,因为ajax调用了相同的模式。

我正在使用ajax函数来获取css类中的内容,请看下面的代码及其模式

代码语言:javascript
复制
        $("#poto").click(function (e) {
$("#loadimg").show(); 
jQuery.ajax({ url: "photos.php", 
         cache: true,
            success:function(response){
                $(".homefeed").html(response);
                  $("#elementcontainer").show();
                $("#loadimg").hide(); 
            },
            });return false;
    });


            $("#mkt").click(function (e) {
$("#loadimg").show(); 
jQuery.ajax({ url: "newm.php", 
         cache: true,
            success:function(response){
                $(".homefeed").html(response);
                  $("#elementcontainer").hide();
                $("#loadimg").hide(); 
            },
            });return false;
    });

        $("#invite").click(function (e) {
$("#loadimg").show(); 
jQuery.ajax({ url: "invite/index.php", 
         cache: true,
            success:function(response){
                $(".homefeed").html(response);
                $("#loadimg").hide(); 
            },
            });return false;
    });

.10倍于此

是否有任何方法将其缩小,因为在.homefeed类中加载内容需要花费太多的时间,而且占用大量的字节/

有什么办法可以实现吗?谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-10 14:40:06

声明一个新函数,它将处理ajax请求和成功响应:

代码语言:javascript
复制
function ajaxLoadImage(url, showElementContainer) {
    $("#loadimg").show(); 
    jQuery.ajax({ 'url': url, 
        cache: true,
        success: function(response){
            if(showElementContainer) {
                $("#elementcontainer").show();
            }
            $(".homefeed").html(response);
            $("#loadimg").hide(); 
        },
    });
    return false;
}

然后,当您绑定单击事件时,只需执行:

代码语言:javascript
复制
$("#poto").click(function (e) { 
    ajaxLoadImage("photos.php", true);
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27877288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档