脚本宝典收集整理的这篇文章主要介绍了前端开发必备常用工具函数小结,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
1、时间格式化等方法
推荐使用 moment.js 的库文件
2、模板、循环、MAP等方法使用
underscode.js 的方法
3、表单序列化成JSON
$.fn.serialize
JSON = function() {
VAR serializeObj = {};
var array = this.serializeArray();
var str = this.serialize();
$(array).
each(function() {
if (serializeObj[this.n
ame]) {
if ($.isArray(serializeObj[this.name])) {
serializeObj[this.name].push(this.value);
} else {
serializeObj[this.name] = [serializeObj[this.name], this.value];
}
} else {
serializeObj[this.name] = this.value;
}
});
return serializeObj;
};
4、字符串截取使用……填补
String.
PRototy
PE.strcut = function(number) {
var length = this.length;
var
tmp = this.s
ubstr(0, number);
if (this.length > number) {
tmp += "…";
}
return tmp;
}
5、时间格式为,xxxx 天,xxx分钟前,日期
Date.prototype.Format = function(fmt,
current) {
if (current) {
var diff = current - this.getTime();
if (diff < 5 * 60 * 1000) {
return "刚刚";
}
if (diff < 60 * 60 * 1000) {
return (Math.floor(diff / (60 * 1000))) + "分钟前";
}
if (diff < 24 * 60 * 60 * 1000) {
return (Math.floor(diff / (60 * 60 * 1000))) + "小时前";
}
if (diff < 30 * 24 * 60 * 60 * 1000) {
return (Math.floor(diff / (24 * 60 * 60 * 1000))) + "天前";
}
if (diff < 12 * 30 * 24 * 60 * 60 * 1000) {
return (Math.floor(diff / (30 * 24 * 60 * 60 * 1000))) + "月前";
}
if (diff > 12 * 30 * 24 * 60 * 60 * 1000) {
return (Math.floor(diff / (12 * 30 * 24 * 60 * 60 * 1000))) + "年前";
}
}
var o = {
"Y+": this.getFullYear(), //月份
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.
test(fmt)) fmt = fmt.replace(RegE
xp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length
== 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
};
6、解析URL
function parseUrl() {
var arr = location.se
Arch.spl
IT('?')[1].split('&');
var params = {};
for (var i = 0, l = arr.length; i < l;
i++) {
var param = arr[i].split('=');
params[param[0]] = param[1];
}
return params;
}
7、获取get参数
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&
#]*)"),
results = regex.exec(location.search);
return results
=== null ? "" : decodeURI
component(results[1].replace(/\+/g, " "));
}
8、函数节流,让频繁事件触发更稀疏提高性能,例如及时搜索功能。使用方法为fn 为事件响应函数,delay 为间隔时间,调用 throTTLe(fn,delay) 返回一个新的函数给事件即可
function throttle(fn, delay) {
var timer = null;
return function() {
var context = this,
args =
arguments;
clearTimeout(timer);
timer = setTimeout(function() {
fn.apply(context, args);
}, delay);
};
}
9、防止表单多次提交,和9中一样,返回一个新的函数
/**
* 防止多次点击函数
*
* fn 完成
时调用回调
* function fn(event,end) {
* (typeof end === "function")
&& end(); // 操作完成
* }
*/function noRepeateTap(fn) {
var $obj;
return function(event) {
$obj = $(this);
if ($obj.data("loadding") === "true") {
return;
}
$obj.data("loadding", "true").addClass('loadding');
fn.call(this, event, function end() {
$obj.data("loadding", "").removeClass('loadding');
return;
});
}
}
第9个的使用例子
//
绑定事件$(cont
ainer).on('click', '.
BTn-cancel', noRepeateTap(cancel));
// 事件响应函数
function cancel(event, end) {
event.p
reventDefault();
// 模拟异步请求
setTimeout(function(){
end(); // 需要手动调用注入的完成函数,通知完成,函数自动添加loadding class 类,用于
调整样式,完成后自动移除
},5000)
}
脚本宝典总结
以上是脚本宝典为你收集整理的前端开发必备常用工具函数小结全部内容,希望文章能够帮你解决前端开发必备常用工具函数小结所遇到的问题。
如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。