1.jquery方式

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ajax加载loading图标</title>
</head>
<body>
<form id="test-form" action="test.php" method="post">
<input type="text" name="username">
</form>
<button id="btn">提交</button>
<div id="loading"></div>
<div id="res"></div>
</body>
<script>
$('#btn').click(function(){
$.ajax({
url:$('#test-form').attr('action'), //发送后台的url
type:'post',
data:$('#test-form').serialize(), //序列化表单内容
dataType:'text', //后台返回的数据类型
timeout:15000, //超时时间
beforeSend:function(XMLHttpRequest){
$("#loading").html("<img src='./images/loading.gif' />"); //在后台返回success之前显示loading图标
},
success:function(data){ //data为后台返回的数据
$("#loading").empty(); //ajax返回成功,清除loading图标
$('#res').html('ajax请求成功!');
}
});
});
</script>
</html>

2.js方式

xmlhttp.onreadystatechange = function(){
var loadingdiv = document.getelementbyid("loading"); 
loadingdiv.innerhtml = "loading..."; 
loadingdiv.style.right = "0"; 
loadingdiv.style.top = "0";
loadingdiv.style.display = "block"; 
//load completed
if(xmlhttp.readystate == 4) {
loadingdiv.style.display = "none"; 
loadingdiv.innerhtml = ""; 
if(xmlhttp.status == 200) {
var str = xmlhttp.responsetext;
/* do something here ! */
}
else
alert("error!" + "nstatus code is: " + xmlhttp.status + "nstatus text is: " + xmlhttp.statustext);
}
}

最新文章

  1. 产生某个区间的随机整数 int #Java
  2. 材价看板(2)- 运行两周的kanban,改进的起点
  3. iOS开发——运行时OC篇&amp;使用运行时获取系统的属性:使用自己的手势修改系统自带的手势
  4. Reservoir Sampling - 蓄水池抽样
  5. H - Frequent values
  6. jni 之helloworld
  7. Xcode7 低版本iOS系统上下有黑边的问题
  8. windows mysql 中文乱码和中文录入提示太大错误的解决方法
  9. ZOJ-2587-Unique Attack(最小割的唯一性)
  10. Sicily-1438
  11. RedHat 7 常用命令总结
  12. 201521123020《Java程序设计》第2周学习总结
  13. Bootstrap-datepicker3官方文档中文翻译---概述(原版翻译 http://bootstrap-datepicker.readthedocs.io/en/latest/index.html)
  14. [物理学与PDEs]第4章习题2 反应力学方程组形式的化约 - 能量守恒方程
  15. ip xfrm命令是做什么的?
  16. Eclipse中避免修改后台代码后手动install和重启
  17. python 列表常用操作(二)
  18. Linux 文件系统结构、磁盘的管理
  19. hadoop系列 第二坑: hive hbase关联表问题
  20. Pupet自动化管理环境部署记录

热门文章

  1. 一小时学会ECMAScript6新特性
  2. Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
  3. 教你用python写:HDU刷题神器
  4. ACM退役帖 -- 未真正开始也不会结束
  5. 算法竞赛入门经典 习题2-10 排列(permutation)
  6. Sublime Text3使用汇总
  7. SpringMVC详解(一)------入门实例
  8. Linux 磁盘分区修改与管理
  9. JS非空验证及邮箱验证
  10. Nginx代理TCP服务