先前写工具类都是自定义类,直接prototype,jQuery扩展这一块儿,一直也没写过,刚好今天有空,写个试试。
已经有很多人对jQuery,jQuery.fn,jQuery.fn.extend详细说明过了,此处不再赘述,直接上代码。
jQuery.ibt = {
// 定义全局常量
showLoading : function (mask) { // 显示遮罩层
var _html = "";
if (mask) {
_html = "
} else {
_html = "
}
$("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();
}
});
},
};
学习之后的实践,欢迎拍砖。
Ajax-05 使用XMLHttpRequest和jQuery实现Ajax实例
需求: (django)使用XMLHttpRequest和jQuery实现Ajax加法运算 url.py: from django.conf.urls import url from hello im ...
JQuery实现AJAX实例
jQuery Ajax 实例 ($.ajax.$.post.$.get) 转 Jquery在异步提交方面封装的很好,直接用AJAX非常麻烦,Jquery大大简化了我们的操作,不用考虑浏览器的诧异了. ...
JavaScript强化教程——jQuery AJAX 实例
什么是 AJAX?AJAX = 异步 JavaScript 和 XML(Asynchronous JavaScript and XML).简短地说,在不重载整个网页的情况下,AJAX 通过后台加载数据 ...
jQuery AJAX实例
jQuery Ajax 实例演示