你好,伙计们,因为内存和时间问题,我只想缩小这段代码,因为ajax调用了相同的模式。
我正在使用ajax函数来获取css类中的内容,请看下面的代码及其模式。
$("#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类中加载内容需要花费太多的时间,而且占用大量的字节/
有什么办法可以实现吗?谢谢
发布于 2015-01-10 14:40:06
声明一个新函数,它将处理ajax请求和成功响应:
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;
}然后,当您绑定单击事件时,只需执行:
$("#poto").click(function (e) {
ajaxLoadImage("photos.php", true);
});https://stackoverflow.com/questions/27877288
复制相似问题