jQuery 扩展 【ajax实例】
2016-05-20 18:08
393 查看
先前写工具类都是自定义类,直接prototype,jQuery扩展这一块儿,一直也没写过,刚好今天有空,写个试试。
已经有很多人对jQuery,jQuery.fn,jQuery.fn.extend详细说明过了,此处不再赘述,直接上代码。
学习之后的实践,欢迎拍砖。
已经有很多人对jQuery,jQuery.fn,jQuery.fn.extend详细说明过了,此处不再赘述,直接上代码。
jQuery.ibt = { // 定义全局常量 showLoading : function (mask) { // 显示遮罩层 var _html = ""; if (mask) { _html = "<div id='pop_mask' style='width: 100%;height: 100%;position: fixed; top: 0;left: 0;background-color: rgba(0,0,0,0.5);z-index: 99;display:none;'>"; } else { _html = "<div id='pop_mask' style='width: 100%;height: 100%;position: fixed; top: 0;left: 0;background-color: transparent;z-index: 99;display:none;'></div>"; } $("body").append(_html); $("#pop_mask").fadeIn("fast"); }, hideLoading : function () { // 隐藏遮罩层 $("#pop_mask").fadeOut("fast"); }, handleError : function () { // 处理错误 }, ajax : function (opt) { // 自定义ajax请求 var defaults = { // 默认值 type : 'get', mask : false, //蒙板 async : true, // 异步 cache : true, // 缓存 dataType : 'json', // 返回数据类型 timeout : 8000, // 最长请求时限 contentType : 'application/json', // 数据格式 }; var opts = jQuery.extend(defaults, opt); jQuery.ajax({ url : opts.url, type : opts.type, data : opts.data, async : opts.async, cache : opts.cache, dataType : opts.dataType, timeout : opts.timeout, contentType : opts.contentType, success : opts.success, beforeSend : function () { $.ibt.showLoading(opts.mask); }, complete : function (res) { if (res.statusText == "timeout") { console.error("the request timeout"); } $.ibt.hideLoading(); } }); }, };
学习之后的实践,欢迎拍砖。
相关文章推荐
- Jquery插件 easyUI属性汇总
- jquery源码分析
- 用jQuery写扩展插件实现语音播报
- struts2+jquery+json集成
- 50个必备的实用jQuery代码段
- jQuery自动补全小数
- jQuery基础的工厂函数以及定时器的经典实例分析
- jQuery实战学习笔记(三)--事件处理
- jQuery基本选择器(实例及表单域value的获取方法)
- jQuery的实例及必知重要的jQuery选择器详解
- jquery 页面滚动到底部自动加载插件集合
- jQuery基础——样式篇
- jquery获取checkbox状态
- jquery.nicescroll.js可全屏可改滚动条颜色的滚动条插件-推荐
- jquery 常用方法及事件操作 $工具方法
- jquery的checkbox,radio,select常用用法
- jQuery 查找on事件绑定元素的被绑定元素方法
- jQuery不同版本共存
- jquery-fullpage-js制作页全屏滚动插件
- jquery.roundabout.js图片叠加3D旋转插件多功能图片翻转切换效果